How do I pass simple Haskell variables to a Heist template? - haskell

First off, I'm very new to Snap and Heist :)
In all templating engines that I have used, there is always a way to pass a variable from the render function to the template. The template can then display the variable in its place. Now, I do understand that Heist is particularly strict, but I'm not even trying to do a loop or an if/else here, just display a random number. I imagine this is a pretty basic thing, but I haven't found anything in the docs that shows how this can be done without using splices.
So in short, is it possible to have a Snap handler that generates a number, then passes it to a Heist template to render, without using splices? If yes, please give me some example code, if no, please show me the simplest way it can be done.

Splices are the way that you pass Haskell information to a template. That's the way you have to do it.

Related

MediaWiki QuickTemplate class - Where to find references?

I'm new to MediaWiki skin design. I have found these two functions being used heavily.
QuickTemplate::html($str)
QuickTemplate::text($str)
These are used in execute() function in the custom Template class, as $this->html($str) and $this->text($str)
I'm pretty ok with their functionality. But, so far, I haven't found a reference for the list of string arguments fed through the $str parameter. I've seen them being used in templates with various arguments like, $this->html( 'headelement' ), or $this->text( 'pageLanguage' ). My question is how do we exactly know the the argument is 'headelement' ? Is there a complete list of such arguments? Not sure whether I'm missing some part of the documentation.
Well, did you try the QuickTemplate Class Reference and Using QuickTemplate in extensions?
The functions html() and text() don't really do anything, they seem just a way to supposedly "abstract" the handling of HTML strings vs. all other strings: text() escapes the HTML a bit. Mostly a historical thing.
As the manual says, please don't use QuickTemplate :), follow the skinning template.

Snap: Heist templates path

How can I change the default templates path?
The initialization function takes path relative to "projectroot/snaplets/heist"
Which is heistInit "templates". So I end up with templates located in "projectroot/snaplets/heist/templates"
I would like my templates in "projectroot/resources/templates" instead.
Is this possible? How do I pass it to the initializer?
Thanks
You can't. Snaplets have to use that directory hierarchy otherwise they won't be fully composable...i.e. what would happen if your app was sub-snaplet of another application. When we first released snaplets, we actually did it the way that you're asking for here. It seemed fine until we tried more complex snaplet hierarchies. If you want to simplify the default as much as possible, you can use heistInit "", which will as you noted store your templates in snaplets/heist.
If you REALLY want to put your templates there, you could add that to the Heist snaplet as another template location using the function addTemplatesAt. But you can't prevent the Heist snaplet's initializer from also looking in snaplets/heist.

Jade to Dust Parser?

I recently wrote using Jade Template for NodeJS. When talking with my manager.. I found that we use Dust within company. Thus I was required to switch over to dust.
While following the DRY principle.. I don't want to do this manually.
Is there good translator/parser to parse existing Jade template to Dust? I searched online but didn't found.
Additionally, if there's no such template, what about I go and implement one myself? I took compiler course before and thinking this would probably be not-to-hard to implement. But I never tried... and don't really understand the Dust template yet. how do you think of the difficulty of doing one parser myself?

Any way in Expression Engine to simulate Wordpress' shortcode functionality?

I'm relatively new to Expression Engine, and as I'm learning it I am seeing some stuff missing that WordPress has had for a while. A big one for me is shortcodes, since I will use these to allow CMS users to place more complex content in place with their other content.
I'm not seeing any real equivalent to this in EE, apart from a forthcoming plugin that's in private beta.
As an initial test I'm attempting to fake shortcodes by using delimited strings (e.g. #foo#) in the content field, then using a regex to pull those out and pass them to a function that can retrieve the content out of EE's database.
This brings me to a second question, which is that in looking at EE's API docs, there doesn't appear to be a simple means of retrieving the channel entries programmatically (thinking of something akin to WP's built-in get_posts function).
So my questions are:
a) Can this be done?
b) If so, is my method of approaching it reasonable? Or is there something stupidly obvious I'm missing in my approach?
To reiterate, my main objective here is to have some means of allowing people managing content to drop a code in place in their content that will be replaced with channel content.
Thanks for any advice or help you can give me.
Here's a simple example of the functionality you're looking for.
1) Start by installing Low Replace.
2) Create two Global Variables called gv_hello and gv_goodbye with the values "Hello" and "Goodbye" respectively.
3) Put this text into the body of an entry:
[say_hello]
Nice to see you.
[say_goodbye]
4) Put this into your template, wrapping the Low Replace tag around your body field.
{exp:low_replace
find="[say_hello]|[say_goodbye]"
replace="{gv_hello}|{gv_goodbye}"
multiple="yes"
}
{body}
{/exp:low_replace}
5) It should output this into your browser:
Hello
Nice to see you.
Goodbye
Obviously, this is a really simple example. You can put full blown HTML into your global variable. For example, we've used that to render a complex, interactive graphic that isn't editable but can be easily dropped into a page by any editor.
Unfortunately, due to parse order issues, EE tags won't work inside Global Variables. If you need EE tags in your short code output, you'll need to use Low Variables addon instead of Global Variables.
Continued from the comment:
Do you have examples of the kind of shortcodes you want to support/include? Because i have doubts if controlling the page-layout from a text-field or wysiwyg-field is the way to go.
If you want editors to be able to adjust layout or show/hide extra parts on the page, giving them access to some extra fields in the channel, is (imo) much more manageable and future-proof. For instance some selectfields, a relationship (or playa) field, or a matrix, to let them choose which parts to include/exclude on a page, or which entry from another channel to pull content from.
As said in the comment: i totally understand if you want to replace some #foo# tags with images or data from another field (see other answers: nsm-transplant, low_replace). But, giving an editor access to shortcodes and picking them out, is like writing a template-engine to generate ee-template code for the ee-template-engine.
Using some custom fields to let editors pick and choose parts to embed is, i think, much more manageable.
That being said, you could make a plugin to parse the shortcodes from a textareas content, and then program a lot, to fetch data from other modules you want to support. For channel entries you could build out of the channel data library by objectiveHTML. https://github.com/objectivehtml/Channel-Data
I hear you, I too miss shortcodes from WP -- though the reason they work so easily there is the ubiquity of the_content(). With the great flexibility of EE comes fewer blanket solutions.
I'd suggest looking at NSM Transplant. It should fit the bill for you.
There is also a plugin called Shortcode, which you can find here at
Devot-ee
A quote from the page:
Shortcode aims to allow for more dynamic use of content by authors and
editors, allowing for injection of reusable bits of content or even
whole pieces of functionality into any field in EE

Snap: Getting form data and the "if"

I dont seem to be able to understand how to get the form data with heist.
I just started looking into Haskell web frameworks but documentation is a bit ... scarce, to say the least. There is no mention of forms in the skinny heist tutorial on the snap website.
So given a simple html form, how do I get the form data for processing into my handler function? Can anyone point me to a tutorial, google just keeps quiet on this one? Or maybe a short example ...
Also, where do I get information on the conditional control? Say, if I want conditionally include certain parts of the html within my page, how do I do it with heist? Basically, where is the "if"?
Thanks
See Using digestive functors with Heist for a good tutorial on simple forms, and mightybyte's musings about heist. If you're planning on using multipart/form-data use the Snap.Util.FileUploads module.
You could use a Splice to generate content based on a condition.
A Splice returns a list of Node's,
mySplice = do
-- get environment conditions
-- condition could be passed in as a parameter
if condition
then return [] -- do nothing
else return [TextNode "some content"] -- see Text.XmlHtml
You would then bind this splice to a tag using something like:
bindSplice myTag mysplice defaultHeistState
And place <myTag/> in your template. When Heist renders the template, it will evaluate the splice when it encounters the tag and insert the value in its place.

Resources