Question

I am trying to step a bit beyond the basic usage of Smarty and ran into a small problem using the default "capitalize" modifier. I am operating with the assumption that no explicit calls need to be made to use this operator so in my template I have the following:

{* Smarty Part: Add-ins *}

<div class="control-group form-inline">
<label class="control-label">Add-ins:</label>
  <div class="controls ">
    {foreach $add_ins as $add_in}
    <label class="checkbox inline"><input type="checkbox" cf-change="toggle-field::#{$add_in}-add-in"> {$add_in|capitalize}</label>
    {/foreach}
  </div>
</div>

If I were to replace the:

{$add_in|capitalize}

with

{$add_in}

It would work fine but adding the modifier gives me the following error:

PHP Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template [FILE_PATH] on line 7 "&lt;label class=&quot;checkbox inline&quot;&gt;&lt;input type=&quot;checkbox&quot; cf-change=&quot;toggle-field::#{$add_in}-add-in&quot;&gt; {$add_in|capitalize}&lt;/label&gt;" unknown modifier "capitalize"'

Any help would be greatly appreciated.

Was it helpful?

Solution

When using above code from ken you could also/better use

$object->addPluginsDir(LG_FE_DIR . '/templates/plugins_dir');

and then you don't have to take care about default stuff.

OTHER TIPS

In your Smarty directory there should be a plugins directory that contains all of the default plugins, including the usual modifiers.

If you can't fix that problem, remember that you can use any PHP function as a modifier. So in this case you can use ucfirst():

{$add_in|ucfirst}

This is subject to a the state of $security, however. See the docs for details.

The problem comes from registering a directory for your own plugins and not keeping the reference to the default directory. So for instance, I used initialise smarty with something like this:

 public static function init () {
    $object = new Smarty();
    $object->setTemplateDir ( LG_FE_DIR . '/templates/uncompiled' );
    $object->setCompileDir ( LG_FE_DIR . '/templates/compiled'  );
    $object->setCacheDir ( LG_FE_DIR . '/templates/cache' );
    $object->setConfigDir ( LG_FE_DIR . '/templates/configs' );

    return $object;
}

At this point I COULD use the default modifiers of Smarty without problem. Then I decided I wanted my own directory of plugins and addedt the following line to my init() method:

$object->setPluginsDir ( LG_FE_DIR . '/templates/plugins_dir');

Worked great in the sense that I could now call my own plugins but it also broke the default plugins (and modifiers). Why? I guess as soon as you're explicit about the plugin directory the default directory falls out of the scope and therefore you need to specify both. Fortunately that is easy ... you just stick the different directories you want to include into an array. For me it looked like this:

$object->setPluginsDir ( array(LG_FE_DIR . '/templates/plugins_dir', LG_FE_DIR . '/externals/Smarty/libs/plugins') );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top