1

I'm beginning with Craft/Twig and am developing a plugin that gets data from a Restful XML feed, and I've successfully tested it on my template.

Below, I have the working plugin code and a sample of the XML structure. The actual data feed will return anywhere from a few to several hundred 'INTELLCONT' nodes per user ('Record').

In order to improve readability, is it possible to paginate this feed data? Would I implement it from the plugin (php) side or rather the (twig) template side?

Here is the code I have so far:

Twig template:

{% extends "_layout" %}

{% block content %}
    <h1>test</h1>
    {% set name_var = 'john-smith' %}
        {{ craft.aifeed.XmlVariable(name_var) }}   
{% endblock %}

PHP Code (plugin):

<?php
namespace Craft;

    class AiFeedVariable
    {
            public function XmlVariable($optional = null)
        {
    $url = "test_bb.xml";  

    $xml = simplexml_load_file($url);

    $xml->registerXPathNamespace('a','http://www.digitalmeasures.com/schema/data');//
    $xml->registerXPathNamespace('dmd','http://www.digitalmeasures.com/schema/data-metadata');

    $secondary_user_id = $optional;
    $xml_report_abbrev_bb = $xml->xpath('//a:Record[@username="'.$secondary_user_id.'"]');

    if($xml_report_abbrev_bb){
        echo '<br>CONTYPE is...'.$xml_report_abbrev_bb[0]->INTELLCONT->CONTYPE;
        echo '<br>termId is...'.$xml_report_abbrev_bb[0]['termId'].'<br>';
        echo '<br>surveyId is...'.$xml_report_abbrev_bb[0]->attributes('dmd',true)['surveyId'].'<br>';

        $dmd_fields = $xml_report_abbrev_bb[0]->children('dmd',true);

        echo '<br>dmd:IndexEntry is...'.$dmd_fields[0]->attributes()['indexKey'].'<br>';


    } else {
        echo 'XPath query failed b';  
    }

     }
    } 


    ?>

The XML file (test_bb.xml)

<?xml version="1.0" encoding="UTF-8"?>
<Data xmlns="http://www.digitalmeasures.com/schema/data" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" dmd:date="2012-01-03">
    <Record userId="148" username="john-smith" termId="4" dmd:surveyId="12">
        <dmd:IndexEntry indexKey="D" entryKey="Dylan" text="Dylan"/>
        <INTELLCONT id="14" dmd:originalSource="54TEX" dmd:lastModified="2017-04-18T10:54:29" dmd:startDate="2011-01-01" dmd:endDate="2011-12-31">
            <CONTYPE>Sales Tools</CONTYPE>
            <CONTYPEOTHER>Sales History</CONTYPEOTHER>
        </INTELLCONT>
    </Record>
</Data>
2
  • I'm not sure what this has to do with Craft cms. It's more about your XML api and normal php than about Craft. Do you want to know how to create a paginate? There are plenty tutorials. You'll just define an offset and a limit for your XML data and show only records within the range. Feb 6 '18 at 22:24
  • Thanks. Is there anything out of the ordinary PHP pagination code to consider when using the PHP as a Craft cms plugin?
    – buck1112
    Feb 7 '18 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.