Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Using Variable in < % control Page() %> ??

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1923 Views
  • SalvaStripe
    Avatar
    Community Member
    89 Posts

    Using Variable in < % control Page() %> ?? Link to this post

    hey Guys,

    i want to create a new pagetype, that loads just the $Content of another page.
    so, the new pagetype has a new text DB field "showContent"

    <?php
    class CreatePopup extends SiteTree {
       static $db = array(
        'showContent' => 'Text'
       );
       static $has_one = array(
    );

       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Main', new TextField('showContent'), 'Content');
          return $fields;
       }
    }

    class CreatePopup_Controller extends ContentController {
       function init() {
          parent::init();
          
          Requirements::themedCSS("layout");
          Requirements::themedCSS("typography");
          Requirements::themedCSS("form");
       }
    }
    ?>

    in my "CreatePopup.ss" File i can write

    Hello i am loading Content from http://www.yoursite.com/$showContent/

    Then there is no error and the Content of "$showContent" is shown.
    But now, i want to use this code..

    <% control Page($showContent/) %>$Content<% end_control %>

    So this PageType shoud show just the $Content of another Page. ("$showContent" contains the URLSegment of the page what should be loaded). But when i use this Variable in Control Page, i get this ERROR:

    Parse error: syntax error, unexpected '}' in /tmp/silverstripe-cache-srv-www-vhosts-domain.com-httpdocs/.cache.srv.www.vhosts.domain.com.httpdocs.themes.domain.templates.CreatePopup.ss on line 53

    Can someone Help me?

    p.s.:
    i tried to write a function in CreatePopup.php, but i'm not going on with it.. just many errors..

       function showControlContent($showContent) {
          $existingCont = DataObject::get_one("SiteTree", "URLSegment = '$showContent'");
          if($existingCont) {
             $makeControlPageContent = "<% control Page(".$showContent.") %>$Title<% end_control %>";
          return $makeControlPageContent;
          }
       }

  • SalvaStripe
    Avatar
    Community Member
    89 Posts

    Re: Using Variable in < % control Page() %> ?? Link to this post

    hey boys ;)

    I just put the "CreatePopup" pagetyp as childs of the pages..
    so the "CreatePopup" pagetypes just make

    $Parent.Content

    so i dont need variable, dont need extra DB field and its better sortet i think.. (that content what should be shown is popup must have an popupchild ;) )

    P.S.: why is it good, that Variables dont work in <% control Page(..) %> (or code segments like this).. they did not work just even with " /$variable " or " /$variable "..

    want to clear this fact to understand it!

    Attached Files
  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Using Variable in < % control Page() %> ?? Link to this post

    You've found one of the limits of the template engine. It passes everything inside the parenthesises as a string to the method.

    A better way of doing what you were doing would be to use a has_one to SiteTree, something like:

    <?php
    class CreatePopup extends SiteTree {
    static $db = array(
    );
    static $has_one = array(
    'showContent'=>'SiteTree',
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Content.Main', new TreeDropdownField('showContentID', 'showContent', 'SiteTree'), 'Content');
    return $fields;
    }
    }

    class CreatePopup_Controller extends ContentController {
    function init() {
    parent::init();

    Requirements::themedCSS("layout");
    Requirements::themedCSS("typography");
    Requirements::themedCSS("form");
    }
    }
    ?>

    Then in your template you can use

    Hello i am loading Content from $showContent.AbsoluteLink


    and, to get the content

    $showContent.Content

    1923 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.