Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Reusable components

Apologies in advance for this, quite possibly simple, question.

I'm trying to build a simple-ish application in Phalcon which will work as a kind of automatically-updated bulletin board; it's likely to be very simplistic to begin with, but over time will have more and more stuff added to it.

What I'd like to do is to write it in modules. Let's say, in trivial-land, that I want to write two modules. One will simply display the current time, one will be a simple CRUD application on one database table that will do a slideshow of whatever content is held within that table. (show this piece of content, wait 10s, show next piece of content, wait 10s, ... you get the idea).

Is it possible for me to write these as two self-contained items? My thinking would be that the 'clock' component would contain all it's own CSS, JavaScript and HTML output - basically would be a miniature MVC application in itself, albeit a very trivial one. Then the CRUD system would become a second, again very simple, self-contained MVC thingymajig which could also output it's own CSS, JavaScript & HTML. Then I'd combine all of these things together and output them to the screen. Woo!

Anyway, is that what Components are for?



6.0k
Accepted
answer

Hi,

Its still possible you can define component with its own template. All you need is create new View object to reder your custom template

for example you have widget component

<?php

use Phalcon\Mvc\View as View;

class Widget extends Component
{
    public function render()
    {
        $view = new View();
        $view->setViewsDir("your/path/to/your/custom/view");

        // the path will be your/path/to/your/custom/view/widget/lastpost.phtml
        $content = $view->getRender('widget', 'lastpost')

        return $content;
    }
}

Thanks Prasetyo - much appreciated :)