Output index block as PHP Array

birwin's Avatar

birwin

14 Mar, 2014 03:01 PM

Hi there, I am attempting to output an index blocks content as a simple PHP array, essentially converting the XML to PHP
I am able to iterate through the index block content, however the output doesn't seem to correct. For example the index block outputs XML like such:

<system-index-block name="Calling Page - Page Index" type="folder" current-time="1394808748499">
    <system-page id="c0a4fd940a0a4a86551ffcc2215dac23">
        <name>Anthropology</name>
        <is-published>true</is-published>
        <path>/Departments/Anthropology</path>
        <site>Introduction</site>
        <link>site://Introduction/Departments/Anthropology</link>
        <created-by>birwin</created-by>
        <created-on>1394801442189</created-on>
        <last-modified-by>birwin</last-modified-by>
        <last-modified>1394801442189</last-modified>
    </system-page>
    <system-page id="c0a63f800a0a4a86551ffcc285113ace">
        <name>Archaeology and Classical Studies</name>
        <is-published>true</is-published>
        <path>/Departments/Archaeology and Classical Studies</path>
        <site>Introduction</site>
        <link>site://Introduction/Departments/Archaeology and Classical Studies</link>
        <created-by>birwin</created-by>
        <created-on>1394801524601</created-on>
        <last-modified-by>birwin</last-modified-by>
        <last-modified>1394801524601</last-modified>
    </system-page>
    ....

I am able to iterate through this easily in Velocity, and I am attempting to output simply the name and link into a php array, like so:

#set ( $data = $_XPathTool.selectNodes($contentRoot, "//system-page")  )
<?php
$indexArray = array(
    #foreach ( $page in $data )
        ## foreach code
        #if ( $page.getChild("is-published").value == "true" )
            #set ( $name = $page.getChild("name").value )
            #set ( $url = $page.getChild("link").value )
            $array("name"=>"${name}", "url"=>"${url}")
            #if( $foreach.hasNext  )
            ,
            #end        
        #end
    #end
    ); //end array definition
?>

This does create something sort of what I am looking for like so:

<system-region name="DEFAULT">
<?php $indexArray = array(
                                $array("name"=>"Anthropology", "url"=>"site://Introduction/Departments/Anthropology")
                        ,
                        
                                        $array("name"=>"Archaeology and Classical Studies", "url"=>"site://Introduction/Departments/Archaeology and Classical Studies")
                        ,
...

The problem is, I continue to get the tag showing up on the outputted PHP page and additionally the url has not been converted to the proper corresponding Url. I have tried using the [system-view:external][/system-view:external] tags as well as the <!--#passthrough ... #passthrough--> tags but it makes no difference.

If anyone has a suggestion, I'd be happy to hear it.

Additionally in Velocity is there a way to do

foreach($xmlObj as $xmlkey => $value)
   to iterate through xml objects?

Thanks,

Brendon

  1. 1 Posted by Ryan Griffith on 14 Mar, 2014 04:53 PM

    Ryan Griffith's Avatar

    Hi Brendon,

    The problem is, I continue to get the tag showing up on the outputted PHP page

    I have a feeling the <system-region> tag is being added due to the lack of a root element, which creates invalid XML. I was able to find a similar discussion on our forum regarding the extra <system-region> tag.

    One thing we could try is surrounding your region within the Template with a dummy tag that is wrapped in #cascade-skip tags. This would cause Cascade to remove the tag on publish. Perhaps something like the following:

    <!--#cascade-skip--><removed><!--#cascade-skip--><system-region name="DEFAULT" /><!--#cascade-skip--></removed><!--#cascade-skip-->

    additionally the url has not been converted to the proper corresponding Url

    It sounds as though Cascade Server is not treating those links as internal/trackable links. Perhaps try surrounding them with a [system-asset] pseudo tag, which will tell Cascade Server the link is internal.

    Additionally in Velocity is there a way to do ... foreach($xmlObj as $xmlkey => $value)

    Each element within $contentRoot, or those produced using the XPathTool, is a JDOM Element, which has methods to obtain children, attributes and content. For documentation on JDOM Elements and the available methods, please see this page.

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by birwin on 14 Mar, 2014 05:32 PM

    birwin's Avatar

    Hi Ryan, you're absolutely right regarding adding the
    <!--#cascade-skip--><!--#cascade-skip-->
    AND
    <!--#cascade-skip--><!--#cascade-skip-->
    around the <system-region name="DEFAULT" />. That fixed the first problem. I had tried that using <!--#passthrough however I did it within the Velocity script rather than within the Template.

    Regarding the [system-asset] tag. You were also right. You're on fire man haha. I only needed to surround the URL though.

    So for those who wanted the final format based on the XML in the original post:

    Template:

    <!--#cascade-skip--><removed><!--#cascade-skip--><system-region name="DEFAULT" /><!--#cascade-skip--></removed><!--#cascade-skip-->
    

    Format:

    #set ( $data = $_XPathTool.selectNodes($contentRoot, "//system-page")  )
    <?php
    /*Loop through system page nodes, and export as php array element*/
    $indexArray = array(
        #foreach ( $page in $data )
            #if ( $page.getChild("is-published").value == "true" )
                #set ( $name = $page.getChild("name").value )
                #set ( $url = $page.getChild("link").value )
                array("name"=>"${name}", "url"=>"[system-asset]${url}[/system-asset]")
                #if( $foreach.hasNext ),#end
            #end
        #end
    
        ); //end array definition
        echo '<pre>'.print_r($indexArray,true).'</pre>';
        echo '<h2>As JSON</h2>'.json_encode($indexArray);
    ?>
    

    I also attached a text file which has everything I did for an example. Hopefully it helps someone else too.

    I'll look more into the JDOM doc. Much appreciated Ryan!

  3. 3 Posted by Ryan Griffith on 14 Mar, 2014 05:50 PM

    Ryan Griffith's Avatar

    Thank you for following up, Brendon, and for providing your final code samples. I am glad to hear those suggestions did the trick.

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  4. Ryan Griffith closed this discussion on 14 Mar, 2014 05:50 PM.

  5. birwin re-opened this discussion on 14 Mar, 2014 07:57 PM

  6. 4 Posted by birwin on 14 Mar, 2014 07:57 PM

    birwin's Avatar

    Hi Ryan, I just thought I'd post this in the event anyone else would find it handy. It has to do with my foreach question above. I wrote a very simple macro to accomplish what someone might want. Here's the macro

    #set ( $data = $_XPathTool.selectNodes($contentRoot, "//system-page")  )
    
    #macro ( toArray $collection ) 
        #foreach ($child in $collection)
            array("${child.name}"=>array(
            #foreach($attr in $child.children)
                #if ( $attr.name == "path" || $attr.name == "link" )
                    "${attr.name}"=>"[system-asset]${attr.value}[/system-asset]"
                #else
                    "${attr.name}"=>"${attr.value}"
                #end
               
                #if( $foreach.hasNext ),#end  
            #end
            ))
            #if( $foreach.hasNext ),#end
        #end
        
    #end
    

    And here's how it'd be used to output a php array or json

    <?php
    $arr = array( #toArray($data) );
    echo json_encode( $arr );
    ?>
    

    This uses the same index block as my example above, but outputs something like so:

    [{"system-page":{"name":"Anthropology","is-published":"true","last-published-on":"1394826529461","last-published-by":"birwin","path":"Anthropology.html","site":"Introduction","link":"Anthropology.html","created-by":"birwin","created-on":"1394801442189","last-modified-by":"birwin","last-modified":"1394801442189"}},{"system-page":{"name":"Archaeology and Classical Studies","is-published":"true","last-published-on":"1394826529461","last-published-by":"birwin","path":"Archaeology and Classical Studies.html","site":"Introduction","link":"Archaeology and Classical Studies.html","created-by":"birwin","created-on":"1394801524601","last-modified-by":"birwin","last-modified":"1394801524601"}}...]

    From the XML like so:

    <?xml version="1.0" encoding="UTF-8"?>
    <system-index-block name="Calling Page - Page Index" type="folder" current-time="1394826862779">
        <system-page id="c0a4fd940a0a4a86551ffcc2215dac23">
            <name>Anthropology</name>
            <is-published>true</is-published>
            <last-published-on>1394826554342</last-published-on>
            <last-published-by>birwin</last-published-by>
            <path>/Faculty/Faculty of the Arts/Departments/Anthropology</path>
            <site>Introduction</site>
            <link>site://Introduction/Faculty/Faculty of the Arts/Departments/Anthropology</link>
            <created-by>birwin</created-by>
            <created-on>1394801442189</created-on>
            <last-modified-by>birwin</last-modified-by>
            <last-modified>1394801442189</last-modified>
        </system-page>
        <system-page id="c0a63f800a0a4a86551ffcc285113ace">
            <name>Archaeology and Classical Studies</name>
            <is-published>true</is-published>
            <last-published-on>1394826554342</last-published-on>
            <last-published-by>birwin</last-published-by>
            <path>/Faculty/Faculty of the Arts/Departments/Archaeology and Classical Studies</path>
            <site>Introduction</site>
            <link>site://Introduction/Faculty/Faculty of the Arts/Departments/Archaeology and Classical Studies</link>
            <created-by>birwin</created-by>
            <created-on>1394801524601</created-on>
            <last-modified-by>birwin</last-modified-by>
            <last-modified>1394801524601</last-modified>
        </system-page>
    ...
    
  7. 5 Posted by Ryan Griffith on 14 Mar, 2014 08:19 PM

    Ryan Griffith's Avatar

    Thank you again for providing the sample code, Brendon. I am sure someone will find this information to be very helpful.

    Have a great day!

  8. Ryan Griffith closed this discussion on 14 Mar, 2014 08:19 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM