I have a situation in which the user is redirected to a template (say I ).
In template I, there is a condition which would determine if we should render that template or redirect to some other place.
The way I am doing right now is to check for that condition in the Template.I.onCreated() and if that condition is true, then putting window.location.href to the new url.
But this is sometimes causing the template I to appear briefly on screen before redirection.
I need a way to be able to stop this from happening. I thought onCreated() fires before onRendered() so it should have prevented that. But still this does not seem to work.
The simplest way to conditionally render a template in blaze is to wrap it with an {{# if}}:
<template name="parentTemplate">
{{#if someCondition}}
{{> childTemplate }}
{{/if}}
</template>
You can define a helper in js that returns a truthy or falsy value for someCondition. No need to do anything in onCreated.
Changing the url via window.location.href is highly discouraged. This will cause the entire app to be reloaded, including all the subscriptions.
Related
I have a single template that's wired up to show a post if the current user has a post in the collection. If not, it automatically shows a form.
Now I want to add an EDIT functionality. When the user clicks on the Edit button, it
Saves the post text in a variable.
Deletes the post from the collection, thus the template reactively reveals the form again.
Up to this part it all works. How can I then add the text that I just saved in a variable, into the "input" element of the form?
jQuery works for this on the console, but I don't know where to put it in my code.
On Discover Meteor, they use the Router to set the context. I'd like to try my idea with jQuery, if possible.
Thank you. Any suggestions are welcome.
You could keep your post in a session variable and in the edit click handler, set the session variable to the one you're currently viewing.
In the form inputs, you can set the value attributes to their corresponding post values.
ex: <input name="title" value="{{post.title}}" />
and in your template helper
Template.form.helpers({
post: function() {
return Session.get('post')
}
})
I have a "tabbed" application I'm developing.
I had used an array of objects stored in a Session variable to create these tabs and simply referenced it in the template. This renders what I'm looking for successfully and makes sense. The short version is:
<template name="tabs">
{{#each tabs}}
{{> Template.dynamic template=templateName data=getTemplateData}}
{{/each}}
</template>
But it appears to re-render every tab each time I add/remove a tab from the array and re-set the Session variable. I have way too many documents potentially referenced in each tab to make re-rendering all of them regularly a viable solution.
If I'm mistaken or was doing this wrong please let me know.
My second thought is to manually add/remove individual tabs from a regular array and reflect this via good old event-driven methods on the page. I have the template name I wish to use for a given tab and the data that needs to be used in it is stored in a Session variable with a unique name.
This should keep the other tabs from re-rendering when I add/remove another tab, but each tab still be subscribed to its own data individually.
I'm stuck on how to create a template instance via javascript so that I can insert it via jQuery. Thoughts?
Blaze.renderWithData was what I was looking for.
I have the following code inside the twig template file:
<th class="head0">ID</th>
Order is a binary integer value (0/1). What I wanna do is to change the value of that variable everytime I click the link. I also cannot use negation in the controller, because it would change that value everytime I click any link redirecting to this specific route.
Is there a way to do this in twig?
What you want to achieve shouldn't be in Twig file. You can only render the initial values with twig. As you said you want the link to respond to user's click. There's two classic ways to do that:
this can be a Javascript code that respond to click and send an ajax request to your
controller. Then you can persist it or do whatever you want with it.
This is the recommended way.
The click redirect you to a new page and the controller that serves that route has the logic you want that is toggling a value or checking a status.
I've got a small question here: is there a way to do some actions after a template has been rendered ? A sort of Listener or hook which is called after a specific template has been rendered ?
In my case I want to know the last datetime the user visited the homepage. If I persist this value in the Controller, so before the rendering of the corresponding template, I won't be able to use my app.user.getLastHomepageVisit datetime variable in the twig template, as the value of the latter variable would be 'now' which is not what I want. I want to update and persist this value to 'now' after the page has been rendered.
Thanks!
There's a hook on kernel.response:
http://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html
I try to call controller/view in another view. I have a homepage Default:index using a block view of my controller Event and I want to put this block.html.twig in my folder of my controller. In my controller Event, I want an action block, in this way I keep the logic of events, in my controller Event.
How can I do for in Event:index.html.twig call my controller/view ?
I saw the helper render, but I think it makes many requests to include the result.
You seem to be on the right track. When calling sub-renders from a view, you have several options, as detailed here: http://symfony.com/doc/current/book/templating.html
The one I think you're looking for is:
{{ render(controller('YourBundle:Event:index')) }}
which will call the controller action and relevant view.
As an aside, if you want a sub-render, but require no controller logic, use
{{ include('YourBundle:Event:index.html.twig') }}
as this seems to be a lot more lightweight.
If you need to use any of these with parameters, normal format is used.