We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Shortcode Parsing

I have started to develop a CMS that utilises the Phalcon framework for my latest project. I am trying to incorporate a shortcode parsing system so that the CMS users can simply enter [NAME_OF_SHORTCODE] in the page editor to pull in some dynamic content. I figured that having saving the shortcode content in partials would be a good idea, however I am now wondering how I can include the partial from the controller. The current flow is:

  • Content is saved in database
  • On page load, the "PageController" scans the content for a shortcode
  • I now need to know how to "insert" the partial content in this position of the string.

I hope this is clear and that somebody out there is able to help me.

Thanks in advance.



3.1k
edited Oct '14

Hmm, it's somehow clear to me. Looks like you want to "pick" a specific partial in your controller, based on some logic.

I guess you could do this similarly to how you would pick a view component, e.g.:

class SomeController extends Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        $this->view->pick("some-view/index");
        $this->view->somePartial = new DefaultPartial();
        if (something) {
            $this->view->somePartial = new somethingPartial();
        } elseif (somethingElse) {
            $this->view->somePartial = new somethingElsePartial();
        }
    }
}

Where in your view you define some variables to allow the inclusion of particular partials.

Just my 2 cents, haven't played with partials yet, but it seems obvious. Best of luck!

Sven

P.S.: Please, for the life of me: someone explain to me how to render php code in this forum! 3 backticks followed by php (```php) doesn't do it :-/



4.6k

I figured it out before returning to this thread. If anyone else is looking for an easy way to accomplish this I simply done the following:


            $partial_name = "sc_" . strtolower($match); // e.g. sc_promotion_box

            $partial = new \Phalcon\Mvc\View\Simple();
            $partial->setViewsDir("../apps/frontend/views/");

            $partial_html = $partial->render("partials/" . $partial_name, array());  // put variables to be passed in array


3.1k

See "Code Blocks" in the Mark down section: https://forum.phalcon.io/help/markdown

No luck Phalcon, I just cannot get it to work :-/ Tried all sorts of different stuff. This should work according to the docs, no?

e.g.:

<?php class DotConfig extends \Phalcon\Config {
      public function get ($index, $defaultValue) {
          $dotFound = strrpos($index, ".");
          if ($dotFound === false) {
              return parent::get($index, $defaultValue);
          } else {
              $getString = str_replace(".", "->", $index);
              return parent::get($getString, $defaultValue);
          }
      }
  } ?>


3.1k

Nevermind :-/ I checked out how you edited my post and played around a bit more. Seems there can't be a blank space after ``php. Once I removed that space and rearranged the line spacing, the code got formatted.

Thanks Phalcon! Though this could be improved upon a little still ;)