Chicago Boss CSRF and variable passing to template files - erlang

I've two problems with my simple application.
I've read the documents twice, but had no luck to using boss_csrf_filter and passing variables to templates.
First I've done the configuration for using CSRF filter in boss.config file as the following :
{controller_filter_config, [boss_csrf_filter]},
{session_adapter, mock},
{session_key, "_myapp_session"},
{session_exp_time, 525600},
{session_cookie_http_only, false},
{session_cookie_secure, false},
{session_domain, ".myappdomain.com"},
Then, I've add to variable in template (which is under src/view/world/hello.html)
<div class="row">
<div class="12u">
<textarea name="message" id="message" placeholder="placeholder" required></textarea>
{{ csrf_token }}
</div>
</div>
Everthing seems fine, but no any hidden input element appearing. Should I check/configure anything else?
The second problem is with my app is passing variables to templates. My is controller something like the following code :
-module(myapp_world_controller, [Req]).
-compile(export_all).
hello('GET', []) ->
{ok, [{world}]}.
postcontact('POST', []) ->
{redirect, "/",[{contactformsend, "ok"}]}.
The code is redirecting without any value. So, the following code is not working (the same template file with I've mentioned above) :
{% if contactformsend %}
<script>
alert("Hi there is!");
</script>
{% endif %}
What I am doing wrong? How can I fix the problems?

I would propose to split your question into two, because I have answer only for the second part. When you use redirect in controller, you don't pass the variables. In the docs you can read this:
{redirect, Location, Headers::proplist()}
So the third argument is list of headers, not variables passed to templates. It makes sense: the variables will be taken from the controller, that is responsible for rendering "/".

Related

Action cable to a specific div

I am looking to update a specific div that can change depending on the stream.id.
I have the following :
ActionCable.server.broadcast('webconferencier_channel', participants_html: participants_html, stream_id: webconferencier.stream_id)
Where I do pass the stream_id.
I am looking to retrieve it in coffee file like this:
received: (data) ->
# Called when there's incoming data on the websocket for this channel
participantsHtml = data.participants_html
$('#show_participants' + data.stream_id).html(participantsHtml);
And then update the view as:
<div id="show_participants_#{<%= #stream.id %>}">
</div>
I can see the AC sending the div template with the other variables, but i cannot seem to retrieve and update it. I think my coffee syntax is incorrect.
In the view, the div id show up as :
<div id="show_participants_#{67089dbd-9826-41f2-97bc-b774b667ef66}">
</div>
with the UUID being the exact same string that i am passing through the coffee file.
Does anyone know how i could make this work please?
Thank you
You should remove curly braces and octothorp from div's id like show_participants_UUID.
You can add console.log to the coffee to see what's happening inside.

Referencing variable inside template call - sightly

I'm trying to call a variable inside quotes inside a data-sly-template call. I would expect the output to include my string with the appropriate varible inside quotes. Below is the template and the string I'm using to call it:
Template:
<!-- template -->
<template data-sly-template.bnrButton="${ # style='String: button color class (default btn-primary)', classes='String: additional component button classes', text='String: button text (translation key)', defaultText='String: default button text (translation key)', link='String: link target for button (# is default)', fullWidth='Boolean: whether to display button in full width of container', noFollow='Boolean: if button should have a nofollow tag (no is def)', gaCategory='String: GA Category', gaEvent='String: GA Event', gaLabel='String: GA Label', gaAction='String: GA Action'}">
<a class="btn ${style || 'btn-primary'} hidden-print ${classes}${fullWidth ? ' btn-full-width' : ''}" role="button" href="${link || '#'}" rel="${noFollow ? 'nofollow' : ''}" onclick="${gaEvent # context = 'scriptString'}">${text || defaultText # i18n}</a>
</template>
<!-- call made to the template -->
<sly data-sly-call="${patterns.bnrButton # style='btn-secondary-light', classes='btn-sm', text=teaser.libraryButtonHeading, defaultText='View Library', link=teaser.myLibraryLink.href, fullWidth='true', newWindow='false', gaEvent='trackEvent('Test 1','Test 2','${teaser.teaserMessage}')'}"></sly>
If tried a number of combinations to get teaser.teaserMessage to print the authored message, but have been having issues because the patterns call is already inside quotes. I want to be able to pass a single string (called in the template call) to the template that includes all the information it needs for the onclick.
Thoughts?
Thank you.
You can use data-sly-test.variable_name before the template call to store the value into a variable:
<sly data-sly-test.gaEvent="${'trackEvent('Test 1','Test 2','{0}')' # format=teaser.teaserMessage}"
data-sly-call="${patterns.bnrButton # ..., gaEvent=gaEvent }"></sly>

Atom snippet for HTML comments

I was looking through the atom documentation at writing snippets:
Using atom snippets
However, the documentation is too simple for the problem I'm facing. I want to write a snippet that will automatically create a comment that will contain a class name of the HTML element. Let's see an example:
<div class="first-class another-class">
...
</div><!-- first-class another-class -->
I want to have a snippet that will automatically create the comment containing "first-class another-class" when I type, for instance, "comm" after the closing tag of the div and hit TAB. Is this even possible with snippets?
Doesn't your workflow allow you to create the comment together with the preceding tag? In that case, you could use something like this:
'.text.html':
'Snippet Name':
'prefix': 'comm'
'body': """
<div class="${1:classes}">
${2:<!-- your markup here -->}
</div><!-- ${1:classes} -->
"""
Or more advanced:
'.text.html':
'div+comment':
'prefix': 'comm'
'body': """
<${1:div} ${2:class="${3:classes}"}>
${4:<!-- your markup here -->}
</${1:div}>${2:<!-- ${3:classes} -->}
"""
Save this file as (e.g. comm.cson) and put it in the packages directory.

Get children of an XHPChild

I am trying to move my website to Hack and XHP, of course. Below is a structure of what code structure I want to achieve:
<ui:backstageHeader>
<ui:backstageHeader-navItem href="/">stories</ui:backstageHeader-navItem>
<ui:backstageHeader-navItem href="/story/send">send a story</ui:backstageHeader-navItem>
<ui:backstageHeader-navItem href="/aboutus">support</ui:backstageHeader-navItem>
</ui:backstageHeader>
(Note: :ui:backstageHeader-navItem basically renders to <a href={$this->:href}>{$this->getCHildren}</a> so there is not need to attach its class here.)
Below is the code for :ui:backstageHeader:
final class :ui:backstageHeader extends :ui:base {
attribute :div;
children (:ui:backstageHeader-navItem)*;
protected function compose() {
$dom =
<section class="backstage-header">
<div class="container">
<div class="cell-logo">
<a href="/">
<span class="no23-logo-white"></span>
</a>
</div>
<div class="cell-navigation">
</div>
<div class="cell-account">
<div class="cell-login">
<div id="siteNav-login">Autentificare</div>
</div>
</div>
</div>
</section>;
$mainContainer = $dom->getChildren("div")[0];
$cellNavigation = $mainContainer->getChildren("div")[1];
$navItems = <ul class="main-navigation"></ul>;
foreach($this->getChildren() as $child) {
$navItems->appendChild(<li>{$child}</li>);
}
$dom->appendChild($navItems);
return $dom;
}
}
I used the Terminal to debug my code using hhvm -m d <file.php>, and everything was alright there; however, when I get to my browser, I get 500 error header. This is what the log says:
Catchable fatal error: Hack type error: Could not find method getChildren in an object of type XHPChild at /var/www/res/ui/backstage-header.php line 25
The error comes from
$cellNavigation = $mainContainer->getChildren("div")[1];
But, somehow, I need to append ul.main-navigation to div.cell-navigation from my section.backstage-header.
How can I do it?
Don't structure your code this way. Built it up from the inside out, so that you don't have to do a ton of unreadable getChildren calls looking for specific children. Those calls are super hard to read, and super inflexible when you change the structure of your XHP. You wouldn't do something like node.firstChild.lastChild.lastChild.firstChild in the JS DOM, would you? No, there's a better way in JS, to find things by class or ID; in XHP, you can just build it up the right way in the first place!
I'd give you an example of this, but it doesn't look like you actually use $mainContainer or $cellNavigation, so you can just remove those two problematic definitions.
As an aside, you really shouldn't be getting your type errors as catchable fatals from HHVM; this is a last resort sort of check. Try running the hh_client checker directly, maybe even showing its result in your IDE; it will give you a much faster iteration cycle, and much more information than HHVM provides.
From my experience, appendChild is very prone to human error. It's easier to do something like:
$items = (new Vector($this->getChildren()))->map($child ==> <li>{$child}</li>);
return <div id="container">{$items}</div>;
If you want to wrap the children in <li />.
Not sure if that will work but it will be close.
Pro tip: You can assign variables from within an XHP tree.
$root =
<div>
{$child = <span>
Text children
</span>}
</div>;
Now $child is already set to the <span> element.

Grails "bean:input" (bean fields) showing the wrong bean property

Since we've updated to grails 2.0.1 (from 2.0.0) all of our beans shown via bean fields are incorrectly displayed as the first property of that "withBean" field. In the example I've posted below, all of [firstName, lastName, dateOfBirth, contactNumber] are shown as just 'firstName' (we know it's not just the messages which are wrong because otherwise the 3rd property (dateOfBirth) would be a date picker, not just a text field).
Any potential workarounds or suggestions?
(plugins.bean-fields=1.0-RC3)
I encountered the same problem, and have a work-around.
I has customised beanfield templates extracted into a gsp template called /shared/_beanfieldConfig.gsp , which I then included by rendering before calling any beans tags. e.g.
<g:render template="/shared/beanFieldConfig" />
<bean:withBean beanName='command'>
<bean:input property='username' />
This worked nicely in 1.3.7, and meant I could share beanFieldConfig between views.
When upgrading to 2.0.3, I enountered the same issue as the original question. I've found I can work around this by inlining the content of my _beanFieldConfig in each view.
Yuk, but at least it means I don't need rewrite all my views to use the replacement plugin (yet).
(edit)
Interestingly, although beanField config in a render'd template doesn't work, sticking it in a taglib DOES.
so, while previously I had in beanFieldConfig
<bean:inputTemplate>
<div class='input ${errors ? 'errors' : '' }'>
${label}
${field}
<g:if test="${errors}">
${errors}
</g:if>
</div>
</bean:inputTemplate>
If I define the equivalent tag -
def beanFieldConfig = {
bean.inputTemplate { m ->
m.with {
""" <div class='input ${errors ? 'errors' : '' }'>
${label}
${field}
${errors ?: ''}
</div>"""}
}
}
and then in my gsp replace <g:render template="/shared/beanFieldConfig" /> with <g:beanFieldConfig/>, it works.

Resources