If I want to use the g.link tag to link the text "foo" to the list action of the book controller, I can do that with:
<g:link action='list' controller='book'>foo</g:link>
The code above shows the "tag syntax", but how can I do the same thing using the "method call syntax"? I tried the following:
g.link(action: 'list', controller: 'book', {'foo'})
but it doesn't work. The problem is that I can't figure out how to pass a static piece of text for the body parameter. In the example above, I've tried putting the text in a closure, but this didn't work.
If the code is e.g. inside controller, you can use:
link( controller:'book', action:'list' ) { 'foo' }
It is not necessary to use the 'g' namespace, it is automatically injected.
Like this:
g.link ([uri:'/'], {"hello"})
Related
I've got a list view and for each line I want a link to another list view showing the related results. The createlink line I've finally managed to construct looks like this
my link
This generates the link
http://localhost:3278/FARTFramework/runResults/listSpecific/testExecQueueInstance.id
I know that testExecQueueInstance.id is 22 and I want the link to actually look like this
http://localhost:3278/FARTFramework/runResults/listSpecific/22
What am I missing?
The idea is that I then have a controller like so which should then list those items matching that id...
def ListSpecific(int id){
render(view: "list", model: [runResultsInstanceList: RunResults.findAllByTestExeQueueID(id, [sort:"testExecTime", order: "desc"]), runResultsInstanceTotal: RunResults.count()])
}
You are using an apostrophe on map of params.
You should try this.
my link
enjoy.
I can call the closure of a controller in a gsp view using the following link:
<g:link url="${request.contextPath}/data/${params.name}/myController/myClosure">
Text Goes Here
</g:link>
However I would like to use remoteLink because I don't want to render anything, just perform a task. Am I correct to want to use remoteLink?
Unfortunately remoteLink does not have a url attribute. So I use controller and action:
<g:remoteLink controller="myController" action="myClosure">
Text Goes Here
</g:remoteLink>
Which unsuccessfully uses the url "/myApp/MyController/MyClosure", which is different from the url that works.
So my questions are,
How did my controller get that longer url? It does not appear in my URLMappings. What controls this?
Is there anyway I can use remoteLink to access that url?
Thank you!
The URL mapping must be named:
static mappings = {
name <mapping name>: <url pattern> {
// …
}
}
Then you can do:
<g:remoteLink mapping="<mapping name>" controller="myController" action="myClosure">
Text Goes Here
</g:remoteLink>
I have a g:select statement that looks like the following:
<g:select id="gearbox" name="gearbox.id" from="${com.nppc.mes.energyusage.Gearbox.list()}" optionKey="id" optionValue="${ {"${it.gearboxType} - (${it.gearboxRatio})"} }" required="" value="${gearboxVoltageInstance?.gearbox?.id}" class="many-to-one"/>
I have added an optionValue attribute: optionValue="${ {"${it.gearboxType} - (${it.gearboxRatio})"} }"
This works as I want.
However, I want to show my domain object, Gearboxes the same everywhere. I have created a template, and am able to use the g:render tag on my show.gsp.
What I can't figure out is how to get something like this to work:
optionValue="<g:render template="/shared/gearbox" model:="[gearbox:it]"/>"
Is it possible to use templates to generate the content that goes into an optionValue?
You could hack around it, but it feels like bad design to me.
The right way to do something like is seems to be to add a helper method to your Gearbox domain class
static transients = [ 'renderedValue' ]
def getRenderedValue(){
"$gearboxType - $gearboxRatio"
}
Then, you can just call optionValue="${ it.renderedValue }".
Also, if you specify a toString() in your domain class, this would be used to generate the default rendered value.
Tags can be used as regular functions in Groovy code. Like:
optionValue="${g.render(template: '/shared/gearbox', model: [gearbox: it])}"
I have this line in a template:
jander
So as you can guess when I click that link, it executes an action. Inside that action I have this:
var_dump($request->getParameter('aux'));var_dump($request->getParameter('sf_subject'));die("fasf");
It's printing 4 and NULL. Why NULL? I expected the $category object was showed..
You can't get the sf_subject parameter that way. You can only use the sf_subject with the sfDoctrineRoute or sfPropelRoute. In your code, you can then get the selected object with $this->getRoute()->getObject().
What is the html representation of your first line?
Seems like in your template $category is not defined, so the < a > should looks like
foo.php?aux=4&sf_subject=null or
foo.php?aux=4
Define the category in your template :-)
I'm wondering if anyone's got any good advice/experience regarding setting dynamic meta titles in Symfony?
Currently, the solution I'm aware of would be to use the following code to set a title individidually in each action:
$this->getResponse()->setTitle('This is a title');
Because I also need translated titles, I could call the i18n helper in the action to get them included in the extracted XLIFFs. No special SEO stuff needed, just a clean title.
However, the above does require that I tweak every single action separately. View.yml is not suitable as I frequently have multiple actions/templates per module.
Is anyone aware of a better approach in Symfony or is this indeed the right/only way to go?
Thank you.
You should use slots.
In your layout <head> tag:
<title><?php echo get_slot('page_title', __('Default page title here')) ?></title>
And in an action template:
<?php slot('page_title', __('Action page title goes here')) ?>
I think writing separate titles in every action is OK. But if you want to add some global prefix you can use something like this in layout:
<title>SITE NAME — <?= $sf_response->getTitle() ?></title>
Also you can probably manipulate a title per module using preExecute() method in actions.
I personally like using the yml files, it separates 'configuration' from code
To deal with dynamic titles I do the follow:
in apps/frontend/config/app.yml
all:
title_separator: ' - '
title_default: 'TITLE'
in apps/frontend/config/view.yml
default:
metas:
title: %APP_TITLE_DEFAULT%
If you need to have data from your actions put into the title, create file lib/myActions.class.php with the following content:
<?php
class myActions extends sfActions
{
protected function setTitle($string)
{
$this->getResponse()->setTitle($string . sfConfig::get('app_title_separator') . sfConfig::get('app_title_default'));
}
}
?>
(note: modify this as you like, e.g. put default title at front)
Then change your action.class.php to extend myActions instead of sfActions
class memberActions extends myActions
and whenever you need to change the title, just call this in your action
$this->setTitle('This is how I roll');
and you will get the following title (if using the same config as I did above):
This is how I roll - TITLE
$i18n = $this->getContext()->getI18N();
$this->getResponse()->setTitle('Your title' . ' | ' . $i18n->__('your module name'));