Closed wilcoverhoeven closed 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):
index.php
//[...]
$renderer->render('index', [
'parsedown' => new Parsedown(),
'markdown' => getMarkdownFromDb()
]);
index.jade
h1 My Markdown Content
!= $parsedown->text($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)
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 :)
Thank you for your detailed answer, I will try the methods you suggested.
Feel free to re-open this if there are any further questions :)
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:
As I understand it, tale-jade will parse jade and create php files, so the code above will never work.