Jump to content

Recommended Posts

Posted

Hi there! I've got a question about selectors. I'd like to know if it's possible to use a selector to find pages based on the Content-Type of their specific template. I'd like to use a selector to find all pages that have a template with an "application/json" Content-Type, if such is possible.

Here's the PHP code I'm using for the template that should output my data. The idea is that you can filter, sort and limit stuff using URL variables. Right now, "template" must be specified in the $input variable, or else the json output gets corrupted by the HTML (returned by the render() call on pages with a "text/html" Content Typed template).

I only wish to grab the pages that have a "json/application" Content Type if the "template" input value is empty.

<?php
header('Content-Type: application/json');

$selector = '';
// TODO: start with Content Type filter
if ($input->template) $selector .= ',template=' . $input->template;
if ($input->start) $selector .= ',start=' . $input->start;
if ($input->limit) $selector .= ',limit=' . $input->limit;
if ($input->sort) $selector .= ',sort=' . $input->sort;

$pages = $pages->find($selector);
$n = count($pages);
$output = '[';
?>
<?php for ($i=0; $i<$n; $i++) : ?>
    <?php $output .= $pages[$i]->render(); ?>
    <?php if ($i < $n-1) { $output .= ','; } ?>
<?php endfor; ?>
<?php
$output .= ']';
echo $output;

 

Posted

Hi

 $templates->find("contentType=json");

text/html = html

text/plain = txt

application/json = json

application/xml = xml

Posted

As usual, it was a very simple method I didn't think of myself. Thanks a lot! 

I modified my selector like this:

$selector = '';
if ($input->template) {
    $selector .= 'template=' . $input->template;
} else {
    $selector .= 'template='.$templates->find("contentType=json");
    $selector .= ",template!=data-json";
    $selector .= ",children.count=0";
}
if ($input->start) { $selector .= ',start=' . $input->start; }
if ($input->limit) { $selector .= ',limit=' . $input->limit; }
if ($input->sort) { $selector .= ',sort=' . $input->sort; }
$foundPages = $pages->find($selector);
  • First line does what I just asked: it filters the pages by their template's Content Type.
  • Second line makes sure I exclude the current page's template itself.
  • Third line ensures I grab only the "leaves" of the page tree, and none of the "branches". 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...