changing templates on the fly
  • is there a way to change template inside template code?
    For exaple:
    Default template is "Desktop" but when someone opens site from moblie device o would change the template to "Mobile".


  • 8 Comments sorted by
  • you can try with this (on top, before <html>)

    {{ dynamic }}
    {{ if isset($smarty.request.mobile) ||
          isset($smarty.request.tablet) ||
          isset($smarty.request.phone) ||
            $gimme->browser->browser_working == "webkit" && 
            (
              ($gimme->browser->ua_type == 'mobile' && (empty($smarty.cookies.app_mode) || $smarty.cookies.app_mode !== 'off')) || 
              isset($smarty.cookies.app_mode) && $smarty.cookies.app_mode == 'on'
            ) 
    }}
      {{ include file="mobile/index.tpl" }}
      {{ php }}
        die();
      {{ /php }}
    {{ /if }}
    {{ /dynamic }}
    --
    Ljuba Rankovic
    Senior Front End Developer, Sourcefabric
    ljuba.rankovic@sourcefabric.org

    http://www.sourcefabric.org
    http://www.twitter.com/Sourcefabric
  • Vote Up0Vote Down Andrey PodshivalovAndrey Podshivalov
    Posts: 1,526Member, Administrator, Sourcefabric Team
    actually, I don't like one thing in this example: die()  :)

    better to implement with else:

    ...
    }}
      {{ include file="mobile/index.tpl" }}
    {{ else }}
      {{ include file="index.tpl" }}
    {{ /if }}
    {{ /dynamic }}

  • but this is already in index.tpl, as this is the default template for current issue. so function should die and continue further down with index.tpl code, otherwise loads mobile/index.tpl
    --
    Ljuba Rankovic
    Senior Front End Developer, Sourcefabric
    ljuba.rankovic@sourcefabric.org

    http://www.sourcefabric.org
    http://www.twitter.com/Sourcefabric
  • Vote Up0Vote Down Andrey PodshivalovAndrey Podshivalov
    Posts: 1,526Member, Administrator, Sourcefabric Team
    of course it could be any other file name. It doesn't matter. The main problem is die shouldn't be used in templates. In some cases it generates an errors due to die stops standard code workflow.

  • that is a solution but what if i have custom templates for sections and topics? It would be easier to change whole template than make this trick in every template file.
  • Vote Up0Vote Down Andrey PodshivalovAndrey Podshivalov
    Posts: 1,526Member, Administrator, Sourcefabric Team
    you can use this template switcher for home/section/article (own switcher for each type). Also this approach could be used to differentiate templates by languages or by site name (ie mobile.domain, www.domain)

  • is available something like $gimme->browser->width ?
  • Vote Up0Vote Down Andrey PodshivalovAndrey Podshivalov
    Posts: 1,526Member, Administrator, Sourcefabric Team
    no. The full browser specification is availably only for client side javascript.
    You have several options how to implement it.
    1. Apply proper style after page loading via javascript (by default style could be display:none)
    2. On first page loading set cookie with browser information and reload page. In this case you can use info from cookie inside template.

    Post edited by Andrey Podshivalov at 2012-04-04 04:54:39