Smarty - what is it?

General project discussion. NOT for help questions.
Post Reply
docman
Forum Members
Forum Members
Posts: 103
Joined: Sat Oct 10, 2009 4:25 pm

Smarty - what is it?

Post by docman »

Hi, this may sound like a very rudimentary quesntion, but I cannot figure our what the smarty is, and how is it implemented in CMSMS. For an example, here is the how to use instruction for Custom Content module:

{if $customcontent_loggedin > 0}
  Welcome {$customcontent_loginname}
{else}
  You are not authorized to view this data
{/if}

Or you can use the $ccuser variable for more advanced, and easier to read testing

{if $ccuser->loggedin() && $ccuser->memberof('members') && $ccuser->ipmatches('192.168.0.0/24')}
Welcome logged in local member
{elseif $ccuser->loggedin() && $ccuser->memberof('members')}
Welcome logged in member
{elseif $ccuser->loggedin()}
Welcome user from some other group
{else}
Anonymous user
{/if}
Jos
Support Guru
Support Guru
Posts: 4019
Joined: Wed Sep 05, 2007 8:03 pm

Re: Smarty - what is it?

Post by Jos »

Smarty is the template engine for CMSms http://www.smarty.net/manual/en/
You can customize the cms-output with it.

You seem to want to use the Custom Content module for something. Tell us what you are trying to achieve and we'll help you with it.
docman
Forum Members
Forum Members
Posts: 103
Joined: Sat Oct 10, 2009 4:25 pm

Re: Smarty - what is it?

Post by docman »

Jos wrote: Smarty is the template engine for CMSms http://www.smarty.net/manual/en/
You can customize the cms-output with it.

You seem to want to use the Custom Content module for something. Tell us what you are trying to achieve and we'll help you with it.
Thanks. What I intend to do is simple at this point:

1. I want to create pages that I would like to show only to registered frontend users.
2. I may want to do a twist: to show them only to a group of frontend users.

But I would of courxe want to understand:
1. how to use the smarty codes given as examples with a number of modules of CMSMS (I tend to think they are used similar to {tags}, that is embeded into contents, but I failed trying.

Thanks so much for your help
Jos
Support Guru
Support Guru
Posts: 4019
Joined: Wed Sep 05, 2007 8:03 pm

Re: Smarty - what is it?

Post by Jos »

To begin with your last question:
With smarty you can put dynamic content in a static template. For normal pages, you may have seen the {content} tag or the {title} tag. They will be replaced with values that you have put in the pages fields. Also modules can have such tags, but they are only usable in module-templates.

There are also smarty functions like {if} and {foreach}
If you see a $-sign in those tags, they represent variables.

So for showing content only to authorised users, you provided the code allready:

{if $ccuser->loggedin() && $ccuser->memberof('members')}
Show your content here with {content} if you use this code in a template.
{else}
Message: Please login to access the requested information
{/if}

the variable $ccuser->loggedin() is set to true if the user is logged in.
the variable $ccuser->memberof('members') is set to true if the user is a member of the group named members.

There's a && between them, so both variables have to be true to output the content.
docman
Forum Members
Forum Members
Posts: 103
Joined: Sat Oct 10, 2009 4:25 pm

Re: Smarty - what is it?

Post by docman »

Thank for your reply.

I have tried entering the smarty code (shown above) into a content page. It did not work, because it was rendered as content. Obviously this is not the way to do.

I begin to realize that it need to be entered into a template replacing the {content} tag?

If so, where would the regular content be shown. Also, how can one assign pages to be shown to registered users WITHOUT putting the code of the content into the template.

As you may see, I am missing still out on the practicalities (were to put the code, where the constent, and so forth).
Peciura

Re: Smarty - what is it?

Post by Peciura »

Jos
Support Guru
Support Guru
Posts: 4019
Joined: Wed Sep 05, 2007 8:03 pm

Re: Smarty - what is it?

Post by Jos »

docman wrote: I begin to realize that it need to be entered into a template replacing the {content} tag?
correct. It's possible to put the code in the content-field of a page, but that is more complicated I think.
docman wrote: If so, where would the regular content be shown. Also, how can one assign pages to be shown to registered users WITHOUT putting the code of the content into the template.
It's the most simple to have two templates, one for normal content and one for logged in users. When you create a page, you can choose the template you want for that page.
docman
Forum Members
Forum Members
Posts: 103
Joined: Sat Oct 10, 2009 4:25 pm

Re: Smarty - what is it?

Post by docman »

Thanks. I am getting the idea.

Soon I test it.
docman
Forum Members
Forum Members
Posts: 103
Joined: Sat Oct 10, 2009 4:25 pm

Re: Smarty - what is it?

Post by docman »

docman wrote: Thanks. I am getting the idea.

Soon I test it.
I did test it. I created a page with a template for the registeres users where I replaced the {content} tag by the this

{if $customcontent_loggedin > 0}
  Welcome {$customcontent_loginname}
{else}
  You are not authorized to view this data
{/if}


I logged in and went to the page for the registered content. Instead of showing its content, the alert message was shown indicating that I am not logged in. I went back and checked if I remained logged in. Yes I did remained logged in.

I did investigate a bit further.

I found out that when I first visit the registered page after having goten logged in, instead os showing the content I placed in the body text, it greets me as a logged in visitor. However, if I insert content into the Intro Window, that is correctly shown.

Now if I leave this page and later return to it, (although still being logged in) it displays the error message (from the template tag) that I am not authorized to view this data.

[Still, later] Herueka!

I discovered that this code worked:

{if $ccuser->loggedin() && $ccuser->memberof('members') && $ccuser->ipmatches('192.168.0.0/24')}
Welcome logged in local member
{elseif $ccuser->loggedin() && $ccuser->memberof('members')}
{content}
{elseif $ccuser->loggedin()}
{content}
{else}
Anonymous user
{/if}

By the way: can a menu item be shown only to registered users?
Last edited by docman on Fri Oct 16, 2009 12:11 am, edited 1 time in total.
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: Smarty - what is it?

Post by jmcgin51 »

docman wrote: I discovered that this code worked:

{if $ccuser->loggedin() && $ccuser->memberof('members') && $ccuser->ipmatches('192.168.0.0/24')}
Welcome logged in local member
{elseif $ccuser->loggedin() && $ccuser->memberof('members')}
{content}
{elseif $ccuser->loggedin()}
{content}
{else}
Anonymous user
{/if}
yes, you found (as others have mentioned elsewhere) that the $customcontent_loginname syntax is deprecated.  Hopefully Calguy will remove it from the Help file in an upcoming release, as it does cause confusion.
docman wrote: By the way: can a menu item be shown only to registered users?
yes
docman
Forum Members
Forum Members
Posts: 103
Joined: Sat Oct 10, 2009 4:25 pm

Re: Smarty - what is it?

Post by docman »

Can you give me an example how can one hide/show menu items to FEUs? It would be a great help for me now.

Thanks in advance.
Jos
Support Guru
Support Guru
Posts: 4019
Joined: Wed Sep 05, 2007 8:03 pm

Re: Smarty - what is it?

Post by Jos »

docman wrote: I replaced the {content} tag by the this

{if $customcontent_loggedin > 0}
  Welcome {$customcontent_loginname}
{else}
  You are not authorized to view this data
{/if}
Also, apart from the deprecated code: you must have at least the {content} tag in your template, otherwise you cannot edit the content of pages and you will get problems with modules.

For the menu I used an extra {menu} tag with a startlevel and other parameters (look at the modulemanager help) and put that menu-tag between the customcontent code
NaN

Re: Smarty - what is it?

Post by NaN »

Since this has nothing to do with smarty at all anymore i would suggest you to try this workaround how to create user specific content:

http://forum.cmsmadesimple.org/index.ph ... 185.0.html
Post Reply

Return to “General Discussion”