phpxmlsymfony4sulu

Including properties with xpointer in Sulu templates


I have set up some Sulu templates and I have been able to use the method described here to include the contents of an entire file in multiple templates:

https://docs.sulu.io/en/2.1/book/templates.html

I'm using:

<xi:include href="fragments/common-properties.xml"/

and this works perfectly

However I have then tried to include the properties from another template using the xpointer method (in order that i can include a bunch of common properties across all templates in one file, PLUS define specific properties for the individual templates)

My root template is here: (root-page-test.xml)

<?xml version="1.0" ?>
<template xmlns="http://schemas.sulu.io/template/template"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xi="http://www.w3.org/2001/XInclude"
          xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/template-1.0.xsd">

    <key>my-account-page-react</key>

    <view>pages/my-account-page-react</view>
    <controller>App\Controller\Headless\HeadlessWebsiteController::indexAction</controller>
    <cacheLifetime>604800</cacheLifetime>

    <meta>
        <title lang="en">Test including properties</title>
    </meta>
    
    <properties>
        <property name="header" type="text_line">
            <meta>
                <title lang="en">Header</title>
            </meta>
        </property>

        <xi:include href="include.xml"
            xpointer="xmlns(sulu=http://schemas.sulu.io/template/template)
                      xpointer(/sulu:properties/sulu:property)"/>

    </properties>
    
    

    
</template>

And the template which contains the properties I want to include is here: (include.xml)

<?xml version="1.0" ?>
<template xmlns="http://schemas.sulu.io/template/template"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xi="http://www.w3.org/2001/XInclude"
          xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/template-1.1.xsd">

    <key>include-test</key>
    
    <view>pages/my-account-page-react</view>
    <controller>App\Controller\Headless\HeadlessWebsiteController::indexAction</controller>
    <cacheLifetime>604800</cacheLifetime>
    
    <meta>
        <title lang="en">Test include</title>
    </meta>
    
    <properties>
        
        <property name="url" type="resource_locator" mandatory="true">
            <meta>
                <title lang="en">Resourcelocator</title>
            </meta>

            <tag name="sulu.rlp"/>
        </property>
        
        <property name="title" type="text_line" mandatory="true"> 
            <meta>
                <title lang="en">Title</title> 
            </meta>
            <params>
                <param name="headline" value="true"/> 
            </params>

            <tag name="sulu.rlp.part"/>
        </property>

        <property name="article" type="text_editor">
            <meta>
                <title lang="en">Article</title>
            </meta>
        </property>

    </properties>
</template>

On loading the admin interface I get the following error from one of the ajax requests:

[ERROR 1612] XPointer evaluation failed: #xmlns(sulu=http://schemas.sulu.io/template/template) xpointer(/sulu:properties/sulu:property) (in C:\xampp\htdocs\<vhost>/config/templates/pages\root-page-test.xml - line 26, column 0)↵[ERROR 1604] could not load include.xml, and no fallback was found (in C:\xampp\htdocs\<vhost>/config/templates/pages\root-page-test.xml - line 26, column 0)↵[ERROR 1871] Element '{http://www.w3.org/2001/XInclude}include': This element is not expected. (in C:\xampp\htdocs\<vhost>/config/templates/pages\root-page-test.xml - line 26, column 0)

Both files are in the same directory

Where am i going wrong?


Solution

  • Your XPointer is not correct, because if you start it with a / it means that you are starting at the root of the document. So if you do /sulu:properties/sulu:property the XPointer assumes that there should be a properties tag at the root of the file you are trying to include. My suggestion is that you make a file like that (this is also shown in the last code snippet in our documentation in this heading):

    <?xml version="1.0" ?>
    <properties xmlns="http://schemas.sulu.io/template/template"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/template-1.1.xsd">
        <property name="url" type="resource_locator" mandatory="true">
            <meta>
                <title lang="en">Resourcelocator</title>
            </meta>
    
            <tag name="sulu.rlp"/>
        </property>
            
        <property name="title" type="text_line" mandatory="true"> 
            <meta>
                <title lang="en">Title</title> 
            </meta>
            <params>
               <param name="headline" value="true"/> 
            </params>
            <tag name="sulu.rlp.part"/>
        </property>
    
        <property name="article" type="text_editor">
            <meta>
                <title lang="en">Article</title>
            </meta>
        </property>
    </properties>
    

    This way you also get rid of the unnecessary elements like view and controller tag, which is not needed in this case.

    Alternatively you can also adjust the XPointer of your include:

    <xi:include href="include.xml" xpointer="xmlns(sulu=http://schemas.sulu.io/template/template)xpointer(/sulu:template/sulu:properties/sulu:property)" />
    

    Mind the additional sulu:template at the beginning of the xpointer.