New jrCore_format_string modifier in JR 5.1.0

brian
@brian
12 years ago
10,149 posts
One of the other new changes in JR 5.1.0 that I've been working on is the new jrCore_format_string setup which allows modules to register string formatters that can be used in templates.

Here's the basic idea:

{$variable|jrCore_format_string:$item.profile_quota_id}

Still takes a quota_id as the first parameter. This will handle running all string formatters that have been registered - i.e. "allowed html", "embed", "bbcode", "convert_at_tags", etc. This allows the module developer to just use the jrCore_format_string modifier and not worry about what type of formatting the site might be using, and makes it so you don't have to do:

{$variable|jrCore_format_string:$item.profile_quota_id|jrAction_convert_hash_tags|jrCore_convert_at_tags|jrEmbed_embed|jrCore_clickable_urls|jrForum_bbcode|nl2br}

Some of these could be from modules that are not even installed (and while it won't create a smarty error due to modifications I have made, it's still messy).

A module registers that it has a string format function in it's init like this:

$_tmp = array(
    'wl'    => 'click_urls',
    'label' => 'Make URLs Clickable',
    'help'  => 'If active, URLs entered into the text will be hyperlinked so they are clickable.'
);
jrCore_register_module_feature('jrCore', 'format_string', 'jrCore', 'jrCore_format_string_clickable_urls', $_tmp);

wl - this is the "whitelist/blacklist" name (more on this below)
label - what it is called in the Core Quota Config
help - help text for quota config field

the 4th parameter to the register function is the actual function that is going to be run - i.e.

/**
 * Registered core string formatter - Clickable URLs
 * @param string $string String to format
 * @param int $quota_id Quota ID for Profile ID
 * @return string
 */
function jrCore_format_string_clickable_urls($string, $quota_id = 0)
{ // Convert URL strings return jrCore_string_to_url($string); }

So all it does is run the jrCore_string_to_url on the string, and return it.

Each formatter is called in order to get the final result.

If it turns out you only want to run specific formatters on a variable, you can define them in the whitelist - i.e.

{$variable|jrCore_format_string:$item.profile_quota_id:"bbcode"}

Would only run the bbcode formatter on the variable.

If you wanted to exclude just the bbcode formatter, you could add it to the black list:


{$variable|jrCore_format_string:$item.profile_quota_id:false:"bbcode"}

The idea here is that template designers should not have to worry about the format of the text that is going to be displayed - down the road there could be modules offering Wiki syntax, markdown, spam checking, whatever - we don't want to have to add variable modifiers to each variable for any of these options in the future.

Let me know what you guys think or if you have any questions - it's a bit more complicated than making a simple modifier, but it should make things much easier to support in the long run.

Thanks!



--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net

updated by @brian: 11/05/13 10:52:00PM
michael
@michael
12 years ago
7,799 posts
Love the concept. Seams like a move toward less template alterations to get something going.

Just have to absorb the implementation and ill be good to go I think.
douglas
@douglas
12 years ago
2,804 posts
Sounds good to me. :)


--

Douglas Hackney
Jamroom Team - Designer/Developer/Support
FAQ-Docs-Help Videos

Tags