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

It really sounds like you need to look into template and/or asset libraries, but here are my basic thoughts:

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.

For language-specific text, you'll most likely want to use language files and reference them using the lang() function (or $this->lang->line()).

For password strength specifically, the script Bonfire uses simply defines a "status" on the password field of invalid/weak/good/strong which is indicated through a class applied to the field (and, optionally, an image). 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).

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.

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). 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 (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).

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 mwhitney - 01-21-2015, 02:10 PM

Theme © iAndrew 2016 - Forum software by © MyBB