by
KK » Fri Jun 21, 2013 11:33 pm
Hi,
is there a way for custom fields to be accessible in a PHP block?
If we use cms:php tag to execute PHP, all the editable regions and variables remain available the normal way. For example, suppose we have a custom field named 'my_region', it's value can be made available to PHP as follows -
- Code: Select all
<cms:php>echo("<cms:show my_region />");</cms:php>
or
- Code: Select all
<cms:php>
$my_var = "<cms:show my_region />";
echo '<h1>' . $my_var . '</h1>';
</cms:php>
Just a small caveat - in the examples above, if the value of 'my_region' happens to contain an unescaped double-quote, PHP will throw a syntax error. For example, suppose if the value of 'my_region' is
hello "world",
the $my_var = "<cms:show my_region />"; statement will then evaluate to
$my_var = "hello "world"";
Clearly that is a syntax error for PHP.
To prevent this from happening, we can use the cms:addslashes tag -
- Code: Select all
<cms:php>
$my_var = "<cms:addslashes><cms:show my_region /></cms:addslashes>";
echo '<h1>' . my_var . '</h1>';
</cms:php>
Hope this helps.