Using PHP inside Smarty template files -



Using PHP inside Smarty template files -

the utilize of php within smarty template files has been possible in previous versions of smarty deprecated.

you can still using backwards compatibility wrapper.

this makes using php within template file possible doing:

{php}echo "hello world";{/php}

does know of problems or issues might cause?

to expand comments given total answer, problem embedding arbitrary php code breaks downwards separation between php , smarty.

since smarty compiles straight php, write in smarty can written in pure php, , may or may not end readable, e.g.

<?= $foo ?> {$foo} fine <?php if ( $expr ): ?> hello <?php endif; ?> {if $expr} hello {/if} not bad either but <?= htmlspecialchars(strtoupper($foo ?: 'hello')); ?> {$foo|default:'hello'|upper|escape:html} bit harder on eye

the main advantages see of using smarty (all of voided if utilize {php}):

obvious separation - clear given file considered template, because it's in smarty. conversely, clear output straight within php function wrong, because result should beingness passed template. forced separation - application logic such database connectivity or input validation cannot "leak" view layer if don't allow functions called smarty templates security - can give non-trusted front-end developers / designers access smarty templates , tightly command info , functionality have access to.

the benefit you'd retain if mixed smarty , php parts of smarty syntax more readable php equivalent, modifier illustration showed above.

php smarty

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -