Jump to:

23487 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » SILVERSTRIPE BOOK XML SITE TREE ISSUE

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2 3 4 5
Go to End
Author Topic: 3675 Views
  • servalman
    Avatar
    Community Member
    211 Posts

    SILVERSTRIPE BOOK XML SITE TREE ISSUE Link to this post

    Hi (hope it is the right forum for this question)

    I'm using 2.4 stable

    I've been tryinng to render the site tree in an XML using the recipe in the silvertsripe book (page 355 english version).

    It works fine with the main site tree .

    My question is how do I get to load a translated site tree or how can I know wich language I'm using

    (i'm using the classic Translatable::enable();

    Here is the code I'm using to get the 'main' site tree as xml :

    Thanks for any advice on this .

    <?php
    class FlashTree extends Controller {

    function xml() {
       $rootPages = DataObject::get(
          'Page',
          'ParentID = 0 ' /* AND ShowInMenus = 1 [to swow only visible menus from SS site tree] */

       );
       
       Controller::curr()->getResponse()->addHeader(
          "Content-type",
          "text/xml"
       );
       $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

       $xml .= "<node>\n";
       foreach($rootPages as $page) {
          $xml .= $this->getNodeXML($page);
       }
       $xml .= "</node>\n";
       
       return $xml;
    }
       
    protected function getNodeXML($page) {
       $xml = sprintf(
          "<node id=\"%s\" label=\"%s\" isBranch=\"%s\" classname=\"%s\">\n",
          $page->ID,
          Convert::raw2att($page->Title),
          ($page->Children() && $page->Children()->Count()) ? 'false' : 'true',
          $page->ClassName
       );
       if($page->Children()) {
        foreach($page->Children() as $page) {
           $xml .= $this->getNodeXML($page);
          }
       }
       $xml .= "</node>\n";
       
       return $xml;
    }
    }
    ?>

  • servalman
    Avatar
    Community Member
    211 Posts

    Re: SILVERSTRIPE BOOK XML SITE TREE ISSUE Link to this post

    Hello

    I love silvertstipe but sometimes I feel stuck by things that I think have a way around.

    I know I'm not a proffessional develloper and maybe the answer is easy or as been already answered but I truly tried to find the answer in the forums or doc.

    If it is not possible to know wich language or site tree you're in can somebody tell me so

    Thank you

  • banal
    Avatar
    Community Member
    901 Posts

    Re: SILVERSTRIPE BOOK XML SITE TREE ISSUE Link to this post

    Hi there

    I never used the method described in the book, but I figured out another way (template-based) to render the Site-Tree to XML with greater flexibility. You can read it up here: http://silverstripe.org/template-questions/show/289897?start=0#post290447

    I explained how it works with HTML, but you can easily change the Markup to XML... I used that approach for several Flash Sites already.

    To render the Tree for another language, you could simply use the URL of the translated Site and apply the export there.

    To get the locale you're currently in, use: Translatable::get_current_locale(). You can also use Translatable::set_current_locale(<locale>,<lang>) to set the locale that should be used.

  • servalman
    Avatar
    Community Member
    211 Posts

    Re: SILVERSTRIPE BOOK XML SITE TREE ISSUE Link to this post

    Thank you Banal for the advices

    I tried you solution and this where I got

    Here is my Page.php

    <?php
    class Page extends SiteTree {

       public static $db = array(
       );

       public static $has_one = array(
       );
    static $extensions = array(
    "Translatable"
    );
    public function ExportContent(){
    $viewer = new SSViewer(array('Export' . $this->ClassName, 'ExportPage'));
    return $viewer->process($this);
    }

    }
    class Page_Controller extends ContentController {

       /**
        * An array of actions that can be accessed via a request. Each array element should be an action name, and the
        * permissions or conditions required to allow the user to access it.
        *
        * <code>
        * array (
        * 'action', // anyone can access this action
        * 'action' => true, // same as above
        * 'action' => 'ADMIN', // you must have ADMIN permissions to access this action
        * 'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
        * );
        * </code>
        *
        * @var array
        */
       public static $allowed_actions = array (
       );
    /**
    * Render the page for export
    * @return string
    */

    public function export(){
    return array();
    }
       public function init() {
          parent::init();

          // Note: you should use SS template require tags inside your templates
          // instead of putting Requirements calls here. However these are
          // included so that our older themes still work
          
          Requirements::themedCSS('typography');
          Requirements::themedCSS('form');
          Requirements::javascript("mysite/javascript/jquery.js");

       }
    }

    Here is my template/export/ExportPage.ss

    <div>
    <h1>$Title</h1>
    $Content
    <% if Children %>
    <div class="children">
    <% control Children %>
    $ExportContent
    <% end_control %>
    </div>
    <% end_if %>
    </div>

    Here is my template/export/Page_export.ss

    <!DOCTYPE html>

    <html lang="en">
    <head>
          <% base_tag %>
          <title><% if MetaTitle %>$MetaTitle<% else %>$Title<% end_if %> &raquo; $SiteConfig.Title</title>
          $MetaTags(false)
          <link rel="shortcut icon" href="/favicon.ico" />
          
          <% require themedCSS(layout) %>
          <% require themedCSS(typography) %>
          <% require themedCSS(form) %>
          
          <!--[if IE 6]>
             <style type="text/css">
              @import url(themes/blackcandy/css/ie6.css);
             </style>
          <![endif]-->
       </head>
    <body>
       <div id="BgContainer">
          <div id="Container">
             <div id="Header">
                $SearchForm
                 <!--<h1>$SiteConfig.Title</h1>
              <p>$SiteConfig.Tagline</p>-->
             </div>
          
       

    <div id="HTMLSiteTree">
    <% control Menu(1) %>
    $ExportContent
    <% end_control %>
    </div>
          <div id="Footer">
             <% include Footer %>
          </div>
       </div>
    </body>
    </html>

    Now if i do yoursite.com/somepage/export like in you post I do get a page but it is the same page (the regular page linked to Page .ss) that I usally get and I can't see my <div id="HTMLSiteTree"> with you control inside.

    I did flush and publish the pages

    I think I missed something.

    My ohter question is where does the Translatable::get_current_locale() line .
    In _config.php

    can you help me ?

    Thanks again for your advice.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: SILVERSTRIPE BOOK XML SITE TREE ISSUE Link to this post

    Hi there. This looks all fine. Except one tiny bit: You should put Page_export.ss directly into the templates Folder, not in template/export/Page_export.ss.

    The Translatable set_current_locale and get_current_locale aren't needed.. you could use get_current_locale to get the language the user is in, or you can switch locale manually using set_current_locale (for example in the init method of the controller) when some action is triggered.

  • servalman
    Avatar
    Community Member
    211 Posts

    Re: SILVERSTRIPE BOOK XML SITE TREE ISSUE Link to this post

    Hello

    Thank you for your answer.

    Will try that asap and get back to you .

    About the get_current_locale issue it might help me for something else .

    I have in a template some tabbed content (a Jquery tool I'm using).

    I was about to use a different template for each of my language to change the the tabs text .

    But maybe there is something to do with get_current_locale and some conditional statement ?

    Have you any idea about that (old post, docs, tutorial)?

    Thanks a lot

    (below the template code used for those tabs, the variables like $TextProduct are some HTML fields in the cms)

    <!-- NAV TEXTS PRODUCTS -->

    <ul class="tabs" id="Tabs">
       <li><a href="#">Le Soins</a></li>
       <li><a href="#">Le Bon Geste</a></li>
       <li><a href="#">Le Secret Evidens</a></li>
    </ul>

    <!-- tab "panes" -->
    <div class="panes" id="Panes" >
       <div>$TextProduct</div>
       <div>$GestesProduct</div>
       <div>$SecretProduct</div>
    </div>

    <!-- This JavaScript snippet activates those tabs -->
    <script>
    $(function() {
       $("ul.tabs").tabs("div.panes > div");
    });

    </script>

    <!-- <script>
    ;(function($) {
    $(document).ready(function() {
    alert('jQuery is loaded!');
    })
    })(jQuery);   

    </script> -->
       
    <!-- NAV TEXTES PRODUCTS -->

  • servalman
    Avatar
    Community Member
    211 Posts

    Re: SILVERSTRIPE BOOK XML SITE TREE ISSUE Link to this post

    Hi Banal

    I tried to put Page_export.ss. directly in my template folder.

    I get a result but it is giving me the site tree with all the content of the pages (example sinpset below)

    But my site is not full flash.

    What I need is to get only the site tree with "page name" and "URL"

    <div>
    <h1>TIITLE 1</h1>
    <p><span class="titrecouleur">text</span> <span class="italiquecouleur">text</span></p>
    </div>

    something like this

    <node>
    <node id="24" label="Home" isBranch="true" classname="Page">
    </node>
    ?
    <node id="31" label="SAHOS BY EVIDENS" isBranch="false" classname="RedirectorPage">
    <node id="36" label="Saho Purifiant" isBranch="true" classname="EditorialPage3">
    </node>
    <node id="37" label="Saho Nourrissant" isBranch="true" classname="EditorialPage3">
    </node>
    <node id="38" label="Saho Perfection" isBranch="true" classname="Page">
    </node>
    <node id="39" label="Saho Eclat" isBranch="true" classname="Page">
    </node>
    <node id="40" label="Saho sur mesure" isBranch="true" classname="EditorialP


    </node>

    Maybe I misunderstood something again.
    What I think is that I have to set my XML template in my ExportPage.ss template

    Am I right ?

    this is what I have wrriten in my Page_export.ss

    <?xml version="1.0" encoding="UTF-8"?>
    <% control Menu(1) %>
    $ExportContent
    <% end_control %>
    </xml>

    Thanks again for your help

  • banal
    Avatar
    Community Member
    901 Posts

    Re: SILVERSTRIPE BOOK XML SITE TREE ISSUE Link to this post

    Yes exactly. You can easily use that method to output XML formatted as you wish.
    Just change your template/export/ExportPage.ss template to something like this:

    <node id="$ID" label="$Title" isBranch="true" classname="$ClassName"></node>
    <% control Children %>
    $ExportContent
    <% end_control %>

    Don't know what the "isBranch" is exactly? You could also build a nested site-tree easily...

    3675 Views
Page: 1 2 3 4 5
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.