2

We have a CakePHP 2.5 app for which we'll very soon be building a learning centre/knowledge base. Craft is one of the tools we're considering for that due to its apparent suitability for content editors to create longform, mixed content consisting of text, images and videos without ending up with junk HTML.

For a couple of reasons, it might be preferable for us to have the learning centre content served as part of the app rather than via Craft at a subdomain, so we'd like to know how we would pull in just the generated HTML Content for the actual article content from Craft for embedding within the app's layout.

The learning centre will have four categories, each with an unknown number of articles.

We'd need to be able to reference each article, and maybe each category, from within the app as users can be set parts of the learning centre as tasks.

4

Your app can load up Craft using its bootstrap.php file:

$craft = require 'path/to/craft/app/bootstrap.php';

That file returns a Craft\WebApp instance. From it, you can access all of Craft’s services, including the ElementsService which provides APIs for fetching elements and their content.

Read through our Working with Elements guide to get an idea of how those APIs work.

As a quick example, here’s how you’d get the entries in a section with an ID of 1 with a category selected, with an ID of 5:

$criteria = $craft->elements->getCriteria(Craft\ElementType::Entry);
$criteria->sectionId = 1;
$criteria->relatedTo = array('targetElement' => 5);

$entries = $criteria->find();

To get the HTML for an entry, you could either assemble that in PHP based on the entry’s content fields, but it would probably be easier to just write a template for it, and have TemplatesService::render() render it for you:

$html = $craft->templates->render('path/to/templates', array(
    'entries' => $entries
));

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.