Welcome Guest, Not a member yet? Register   Sign In
Passing language strings to Javascript, view organization help

(01-21-2015, 02:10 PM)mwhitney Wrote: It really sounds like you need to look into template and/or asset libraries, but here are my basic thoughts:
Thanks a LOT for your input. Any thoughts on libraries?

(01-21-2015, 02:10 PM)mwhitney Wrote: Generally, if I need to access PHP functions/variables to generate values for my JavaScript, I create a view which contains a script tag in which I define some variables and echo the values from PHP. Then a JS file can access the variables and deal with them accordingly (or I can keep all of the associated script in the view). An assets library might offer more options for handling this type of issue.
Could you clarify? I'm guessing your HTML looks something like:
<script type="text/javascript" src="/some-codeigniter-method-that-output-JS-vars"></script>
And I'm further guessing that the CI method you are using for your src is aware of the appropriate language via cookie or something? Or perhaps there's a query string or url component that specifies the language?

(01-21-2015, 02:10 PM)mwhitney Wrote: For language-specific text, you'll most likely want to use language files and reference them using the lang() function (or $this->lang->line()).
We currently use such a scheme to fetch language strings. A view might have a line like this in it:
<li class="uk-active"><span><?php echo $this->lang->line('register_heading'); ?></span></li>
Does that sound about right?

The issue is not so much getting language strings, but finding an effective way to inject these language strings into JS and to inject that JS or a reference to a JS-outputting method into a <head></head> section in some view.

(01-21-2015, 02:10 PM)mwhitney Wrote: Password requirements can be spelled out in the view, but further explanation is rarely needed for real-time feedback on password strength (though, if necessary, you could add an indicator of which check failed when returning the result and use that information to display the appropriate text).
Not sure I agree here. If a password is not valid for some reason, it can be a pain in the ass to figure out what is missing. Forcing your users to read that OWASP list of requirements is not exactly rocket science, but it will be a nuisance. I believe "X is an invalid character" is very helpful feedback. This is why I want language strings available in passwords.

(01-21-2015, 02:10 PM)mwhitney Wrote: For the rest, Bonfire uses a template library which simplifies a great deal of the management of potentially-complicated views, but you can easily create a much simpler library or setup some basic functionality in your base controller to handle the major issues.
I was thinking of some modification to my base controller that could load some view, merge it with some associative $data array of values, and return the resulting JS or HTML without putting the resulting merged view code into the output but I'm not really sure if this is feasible or how to do it. Do I need to write my own tool?

(01-21-2015, 02:10 PM)mwhitney Wrote: In most cases, your site is going to use a common template with a content region that changes for each action in your controllers (there can be exceptions, like AJAX methods, but those could just bypass the templating).
I've put some thought into AJAX and it would seem it's a bit more complicated than just bypassing the templating. For example, a base controller that requires users to be logged in may redirect the user to the login page, taking care to remember what page they requested that required login. This redirect behavior is not acceptable for AJAX operations, which should probably just return an error.

(01-21-2015, 02:10 PM)mwhitney Wrote: Instead of loading the same views in every controller/action, you can load them in a library or in a protected method in your base controller (named something like render(), for instance). You can define regions in the template for output of controller/action-specific assets (i.e. CSS and JS), and set them as you see fit
YES YES YES YES any thoughts on accomplishing this quickly/easily?

(01-21-2015, 02:10 PM)mwhitney Wrote: (though, in most cases, your template's head will usually be loaded before your view, so you usually can't load anything into the head from your main content region's view, so you are often limited to adding CSS from the controller in these situations, even if it's only telling the template which CSS files to load).
I don't ever load any views until after my processing is done and it seems pretty obvious to me that one must collect all the data needed for output before attempting to render any output by loading views.

Once you have a system like that in place, it's fairly easy to head down the rabbit hole and add more functionality.

Messages In This Thread
RE: Passing language strings to Javascript, view organization help - by sneakyimp - 01-21-2015, 03:38 PM

Theme © iAndrew 2016 - Forum software by © MyBB