Use smarty var in item url

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

Use smarty var in item url

Post by jack4ya »

Hi in a module (ListIt2) in the detail template I know have:

Code: Select all

<a href="{$item->url|replace:'.com':'.com/en'}">
    {if $display_language == 'de'}Weiter lesen
    {elseif $display_language=='fr'}En savoir plus{/if}
    {else}Read more{/if}
</a>
I also have a custom field called 'display_languange' (which is required to fill) in the module (not page/template level) so I have a param/field: $display_languange.

I would like to use that so I only need one template instead of multiple per language. (and I could use it for any module/lang)

So something like (bit both don't work)

Code: Select all

{$item->url|replace:'.com':'.com/$display_languange'}

{$item->url|replace:'.com':'.com/`$display_languange`'}
Or eval... capture... whatever. I tried enough now. ;) Any help?
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: Use smarty var in item url

Post by Rolf »

languange >> language
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

Re: Use smarty var in item url

Post by jack4ya »

@rolf??

Added info: But I think it just might complicate the matter:
It's a multi lang setup like yours/Gorans with a 'page_lang' param... Site is setup with sectionheaders and I needed the Listit2 module links prefixed with the lang param as well. But it can't read that from page/template level so I added a new field/param/value to Lisitit2 modules also, and *that* value is the one I'm using in ListIt2 links.



I reoconstructed the url... I still need to manually alter my hardcoded domain address but its better than several templates per languange

<a href="{$display_language}/{$item->url|replace:'http://www.domain.com/':''}">link</a>
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Use smarty var in item url

Post by velden »

Smarty variables in CMSMS have a global scope usually meaning they should be available in all template IF the variable exists when processing the template.

No need to hard code the domain name. Example below generates absolute url:

Code: Select all

{$display_language='/'|cat:'nl'}
{root_url assign='rooturl'}

{foreach...}

<a href="{$item->url|replace:$rooturl:($rooturl|cat:$display_language)}">link</a>

{/foreach}
The 'nl' part could be replaced for a smarty variable if it does exist.
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

Re: Use smarty var in item url

Post by jack4ya »

Ah, I guess a missed a bit of the newer/unfamiliar tags.

Time to freshen up! Thnx!
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

Re: Use smarty var in item url

Post by jack4ya »

Sorry can;t get it to work with the cat part.

{capture assign='root_url'}{root_url}{/capture}
{$display_language='/'|cat:'nl'} <--- ? what is this for?

{$display_language} will have one of these values 'en','fr','de'.
So I don't want/need to set it with another value.

I don't use categories at all, but I think you mean because it can be used to construct the url?

<a href="{$item->url|'replace:$root_url':'$root_url/$display_language'}">link</a> <-- DOnt know.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Use smarty var in item url

Post by velden »

Nothing to do with categories, but concatenate.

I think this is more efficient though:

Code: Select all

{*get site's root url*}
  {root_url assign='rooturl'}

{*get language part for url
    this should come from mle module/method actually *}
  {$display_language='nl'}

{*generate new root_url with language part*}
  {$new_rooturl="$rooturl/$display_language"}

{foreach ...}

  {$item->url|replace:$rooturl:$new_rooturl}

{/foreach}
I try to get as much as possible outside the foreach loop for obvious reasons (efficiency).
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

Re: Use smarty var in item url

Post by jack4ya »

I literally reused your code... I already had an root_url defined... but apparent something faulty... so I assign another one.
Post Reply

Return to “The Lounge”