Custom content via Logic Tab

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Locked
DonaldF
New Member
New Member
Posts: 2
Joined: Sun Mar 13, 2016 8:17 pm

Custom content via Logic Tab

Post by DonaldF »

There are other ways of doing this, but this example gives you insight into dynamic execution of smarty tags.

So the idea here is; keep a single template but allow custom content to appear via smarty tags that are specified in the "Smarty data..." entry on the "Logic" tab of your content page editor. For example we want to put some form data on a page. One might create a new template and put the smarty tag after the content definition

Code: Select all

...{$main_content nocache}...
{FormBuilder form='my-form'}...
However this approach creates a never ending list of templates that may need maintenance over time and it would be nice to reduce this to only a handful.

So here is how I approached this problem.

First you need to set up a User Defined Tag:
name: dynamic_smarty
Code:

Code: Select all

$smarty->display('eval:'.$params['smarty_data']);
In your template put the following code before or after your content. You could create a variable to decide but we want to keep this simple for the example:
So let say our content is defined by:

Code: Select all

{$main_content nocache}
Let us put out custom content after with this code:

Code: Select all

{if isset($customcontent)}
    {dynamic_smarty smarty_data="$customcontent"}
{/if}
So far so good, now we need to add the custom content to the page.
In your page content->Logic->"Smarty data or logic that is specific to this page:"
Place the following code:

Code: Select all

{$customcontent = "{FormBuilder form='my-form'}"}
{share_data scope=parent vars='customcontent' scope=global} 
Now when this page gets displayed it will also get the form output as defined by my form template 'my-form'.

I hope this helps people realise just how smart smarty tags are.
It would also be nice if this example was put into the documentation of User Defined Tags.
DonaldF
New Member
New Member
Posts: 2
Joined: Sun Mar 13, 2016 8:17 pm

Re: Custom content via Logic Tab

Post by DonaldF »

Nobody picked up, or could be bothered to point out, the slight incorrectness of my post. Whilst it would all work, dynamic smarty execution would not occur with the code I presented.

Code: Select all

{$customcontent = "{FormBuilder form='my-form'}"}
{share_data scope=parent vars='customcontent' scope=global} 
The above $customcontent variable would acutaully hold the result of the smarty execution of {FormBuilder ....} because it is enclosed in double-quotes. In order for it to hold the smarty tag to dynamically execute it would need to be single quoted.

Therefore the correct code should look like this:

Code: Select all

{$customcontent = '{FormBuilder form="my-form"}'}
{share_data vars='customcontent' scope=global} 
Of course both give the same result, the only difference is the order of execution of the smarty tag {FormBuilder ...}

In the original example the {FormBuilder ...} smarty tag would get executed at the beginning of the template (via tag {process_pagedata}) and the result would be passed through to the {dynamic_smarty ...} tag which would find no smarty tags to execute and simply pass on the HTML to the output.

In the correction example here, the {FormBuilder ...} smarty tag would get executed via {dynamic_smarty ...} tag in the content part of the template.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Custom content via Logic Tab

Post by velden »

However this approach creates a never ending list of templates that may need maintenance over time and it would be nice to reduce this to only a handful.
Consider reading about Template Inheritance: http://www.cmscanbesimple.org/blog/the- ... gn-manager
Locked

Return to “Tips and Tricks”