1

This is probably a very naive question but I am brand new to Craft and as such, have no idea what to research and where...

I have a static website that is built with my own bespoke framework. We have recently decided to add a 'blog' page to this website and resultantly, I am exploring using Craft in order to save me time writing a bespoke solution from scratch.

From what I can see, Craft is extremely flexible and brilliant for professional developers like myself who refuse to work with the likes of WordPress. However, implementing into my framework may just be a step too far in regards to flexibility. Quite simply, I would like to know the following:

  1. Does craft allow me to make calls to get the stored data and then I manually handle the inserting of this data? e.g. Something like: $blog_posts = $craft->getBlogPosts()
  2. If not, is there a way to include certain files at the beginning and end of templates that are outside the craft directory?

Basically, one of the key features of my framework is to handle the generation of all of the JavaScript and CSS links, along with the head section of each of the pages throughout my site. Based on what I have tested, I have managed to get the header working by just adding a PHP 'include' above the craft code in my file, however, the footer (including javascript) does not work because the Craft obviously has some sort of 'exit' call to prevent the code from executing further.

This is what I currently have:

// Define any custom javascript files and css files to be included in the cache
//...

// Generate the document header
include($_PATHS['CURRENT'].'/_includes/_content/header.php');

/*
 * Craft CMS Code to load the blog template
 */

// Path to your craft/ folder
$craftPath = './_craft/';

// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';

if (!is_file($path))
{
    if (function_exists('http_response_code'))
    {
        http_response_code(503);
    }

    exit('Could not find your craft/ folder. Please ensure that <strong><code>$craftPath</code></strong> is set correctly in '.__FILE__);
}

require_once $path;

/*
 * End of Craft CMS Code
 */

// THIS DOES NOT WORK
// Generate the document footer
include($_PATHS['CURRENT'].'/_includes/_content/footer.php');

1 Answer 1

4

You're really fighting against the way Craft CMS does things.

I'd start by having a good read through the Craft Docs first, there really isn't a shortcut. The docs are really great though, and contain everything you're likely to need.

Then, I'd set up a copy of Craft in isolation of your site/framework and just get a feel for how you do things the "Craft way". It is different to just doing HTML & PHP, but not so different that's it going to take you too long to figure it out.

Craft actually uses the Twig tempting language, which is really easy to get going with.

In answer to your specific question...

Does craft allow me to make calls to get the stored data and then I manually handle the inserting of this data?

Yes it does. To output your data, you'd likely use sections/entries in your Twig template. So take a look at the example at the top of this page to see how you'd go about it. You'd input your data via the Control Panel, so have a read here for that.

4
  • Hi Steve, thank you very much for your response! I feared you would say that :-P... In short, one of my developers has already built the page required and linked it into Craft and I am essentially trying to take his code and implement it into my framework. Unfortunately I don't think this is going to work... So, my next solution is to port my entire website to Laravel (something I have been meaning to do), and then implement Craft properly with Laravel (if this is possible). Hopefully this will make the whole process a lot easier :-)
    – Ben Carey
    Mar 6, 2017 at 12:22
  • Just before I mark the answer as accepted, could you possibly tell me if you have any experience with implementing Craft with Laravel? If so, is the process smooth or does it require dirty fixes and hacks? All of which I would be willing to do but I am so limited on time I need to ensure I pick the right path...
    – Ben Carey
    Mar 6, 2017 at 12:24
  • 1
    Hi Ben, I don't have any direct experience with Laravel, but I see no reason you can't integrate the two. Without a bit more info of what's in your Laravel App and what you want from Craft, it'd a bit tricky to say. It comes down to whether you want a Craft CMS site with some extra functionality, or a Laravel-centric site with a Craft CMS blog bolted on. If there isn't too much real integration you could either sit the Craft CMS part in a sub-directory of the Laravel site, or vice-versa. Mar 6, 2017 at 12:37
  • 1
    [Do] you want a Craft site or a Laravel-centric site w/ a Craft CMS blog bolted on? Bingo Steve. I think that can really go into part of your answer above as well, it drives home what I think a lot of people new to this are struggling with. I have nothing to add to this answer but Ben you might want to look into Template Hooks. Those will allow you to call whatever PHP code inside a Craft template, which might be useful when bringing your existing header/footer in. Mar 6, 2017 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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