Talesoft / tale-jade

A complete and fully-functional implementation of the Jade template language for PHP
http://jade.talesoft.codes
MIT License
88 stars 10 forks source link

Add content from database #123

Closed wilcoverhoeven closed 8 years ago

wilcoverhoeven commented 8 years ago

Hi, first of all thank your for this great jade port! I have a question. How would I go about adding and parsing data from a database?

Example:

// in a real project I would use php to get markdown from a database
// for this example I just made a simple variable
<?php $markdown = "#Some Markdown ***Formatted*** text"; ?>

//parse markdown
:md
    $markdown

As I understand it, tale-jade will parse jade and create php files, so the code above will never work.

TorbenKoehn commented 8 years ago

Sadly, filters aren't made for that kind of stuff, especially filters that run through some other parser.

https://github.com/Talesoft/tale-jade/blob/master/Filter.php#L196

Filters get the actual AST node in Tale Jade and the node itself is a Text Node that may contain multiple other text nodes.

The problem basically is, the parsing of the markdown happens before the template is generated, not inside it, the template itself will only receive the already parsed markdown. The markdown parser will never have the possibility to access your $markdown-variable, as it's validated at run-time, when the Markdown-parser is long gone already.

This is needed, as it creates portable templates that don't require the used third-party-library in a filter to function (e.g. stand_alone-mode). It gives you the ability to pre-compile Tale Jade files into a .phtml-file and run them alone, without any other requirements, not even the Tale Jade compiler itself.

Three correct ways to do this (assuming you installed Parsedown anyways, as Tale Jade doesn't include it):

Method 1

Inject the markdown parser

index.php

//[...]

$renderer->render('index', [
    'parsedown' => new Parsedown(),
    'markdown' => getMarkdownFromDb()
]);

index.jade


h1 My Markdown Content

!= $parsedown->text($markdown)

Method 2

Inject a function/helper to parse the markdown

index.php

//[...]

$renderer->render('index', [
    'parse_markdown' => function($markdown) {

        return (new Parsedown())->text($markdown);
    },
    'markdown' => getMarkdownFromDb()
]);

index.jade


h1 My Markdown Content

!= $parse_markdown($markdown)

Method 3

Pragmatic

index.php

//[...]

$renderer->render('index', [
    'markdown' => getMarkdownFromDb()
]);

index.jade


h1 My Markdown Content

!= (new Parsedown())->text($markdown)

Take whatever method you prefer.

There are some other methods, such as creating a static helper that parses your markdown (!= MarkdownHelper::parseMarkdown($markdown))

Don't scare away from using plain PHP in Tale Jade, this is the greatest power it has! It's more pragmatic than some might think and the way you implement things such as helper functions is pretty much up to you, your library and your code style. Use static helpers, global functions, inject single variables as functions or helper objects that contain helper methods, whatever you prefer :)

wilcoverhoeven commented 8 years ago

Thank you for your detailed answer, I will try the methods you suggested.

TorbenKoehn commented 8 years ago

Feel free to re-open this if there are any further questions :)