Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Is it possible to extract content as a smarty variable

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
PierceMcGeough
Smarty n00b


Joined: 10 Jan 2014
Posts: 3

PostPosted: Fri Jan 10, 2014 10:26 am    Post subject: Is it possible to extract content as a smarty variable Reply with quote

I have a layout template (below is an example but much less complex).
Code:
<body>
  <header>
    <nav></nav>
  </header>
  <div class="main">
    <div class="left">
      {include file=$content.left}
    </div>
    <div class="right">
      {include file=$content.right}
    </div>
  </div>
  <footer></footer>
  {include file=$js_scripts}
</body>

The folder structure lets me have the layout file and then folders for the content. The problem with this is I need multiple template pages and once the content requires some inline code it will change. There are also pages that might not require the js_scripts which will result in a smarty error.
Code:
views/layout
    layout_1.tpl
    layout_2.tpl

views/pages/mypage
        left_content.tpl
        right_content.tpl
        js_scripts.tpl

Is it possible to extract content from a page and assign it to a variable instead of including a file which may not even be needed. Im thinking something along the lines of this which will leave me with one page and the content together in once place

Layout.tpl
Code:
<body>
  <header>
    <nav></nav>
  </header>
  <div class="main">
    <div class="left">
      {$content.left}
    </div>
    <div class="right">
      {$content.right}
    </div>
  </div>
  <footer></footer>
  {$js_scripts}
</body>

mypage.tpl
Code:
{extract variable $content.left}
  <h1>Title</h1>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatem, tempore, quae, cum, consequatur quisquam totam facilis illo blanditiis porro in quidem repudiandae necessitatibus dolores eius quam sequi labore impedit id.</p>
{/extract}

{extract variable $content.right}
  <ul>
    <li>Link 1</li>
    <li>Link 2</li>
    <li>Link 3</li>
  </ul>
{/extract}

{extract variable $js_scripts}
  <script src="js/jquery.js"></script>
  <script>
    $(document).ready(function() {
      $('#id').click(function(){
        // run code
      });
    });
  </script>
{/extract}
Back to top
View user's profile Send private message Send e-mail
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Jan 10, 2014 4:47 pm    Post subject: Reply with quote

It looks like a typical use case of template inheritance.

See http://www.smarty.net/inheritance
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP