Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Using closure templates from separate soy files

Josh Wheeler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 16, 2014

have a template called templates.soy and I would like to be able to call a template from a separate file (would like to overwrite that file programmatically from time to time). I can call a template within the same file. Currently I have:

templates.soy
-------------------------------
{namespace plugin.example}

/**
 * Template for the build
 */
{template .anthill}
<html>
    <head>
        ...
    <body>
        <h2>My Form</h2>
        <br />
        {call .form /}  //This works
        {call random.me.moreFields /}  //This throws an error "Attempting to render undefined template 'random.me.moreFields'
    </body>
</html>
{/template}

/**
 * Form info
 */
{template .form}
    //custom html form stuff
{/template}
-------------------------------
selectField.soy
-------------------------------
{namespace random.me}
/**
 * Testing more soy files
 */
{template .moreFields}
   //custom html stuff
{/template}

Is this possible?

--> atlassian-plugin.xml as requested

<?xml version="1.0" encoding="UTF-8"?>

<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.name}" plugins-version="2">
    <plugin-info>
        <description>${project.description}</description>
        <version>${project.version}</version>
        <vendor name="${project.organization.name}" url="${project.organization.url}"/>
    </plugin-info>

    <!-- import from the product container -->
    <component-import key="applicationProperties" interface="com.atlassian.sal.api.ApplicationProperties"/>
    <component-import key="soyTemplateRenderer" interface="com.atlassian.soy.renderer.SoyTemplateRenderer"/>
    <component-import key="userManager" interface="com.atlassian.sal.api.user.UserManager"/>
    <component-import key="projectService" interface="com.atlassian.stash.project.ProjectService"/>
    <component-import key="repositoryService" interface="com.atlassian.stash.repository.RepositoryService"/>
	
	<!-- located under the resources folder -->
	<resource type="i18n" name="i18n" location="stash-profile-plugin" />

	<web-resource key="soy" name="AJS Soy">
        <dependency>com.atlassian.soy.soy-template-plugin:soy-deps</dependency>
        <resource type="download" name="forms.css" location="css/atlassian/forms.css"/>
    </web-resource>
	
    <servlet name="Repository Servlet" key="repository-servlet" class="com.atlassian.stash.plugin.servlet.RepositoryServlet">
        <description key="repository-servlet.description">The Repository Servlet Plugin</description>
        <url-pattern>/example/repository/*</url-pattern>
    </servlet>
    
    <web-resource key="example-resources" name="Example Resources">
    	<transformation extension="soy">
    		<transformer key="soyTransformer" />
    	</transformation>
		<resource type="download" name="selectField-soy.js" location="/templates/selectField.soy" />    	
    </web-resource>

    <stash-resource key="example-soy" name="Example Soy Templates">
        <directory location="/templates/"/>
        <dependency>com.atlassian.stash.stash-web-plugin:global</dependency>
    </stash-resource>
	
	<soy-resource key="aui-experimental-soy-templates" name="AUI Soy Templates">

        <transformation extension="soy">
            <transformer key="soyTransformer" />
        </transformation>

        <!-- Have to duplicate these calls as dependencies don't seem to work with soy-resource -->
        <resource type="download" name="atlassian.soy.js" location="soy/atlassian/atlassian.soy"/>
        <resource type="download" name="form.soy.js"      location="soy/atlassian/form.soy"/>
        <dependency>com.atlassian.auiplugin:ajs</dependency>
        <dependency>com.atlassian.soy.soy-template-plugin:soy-deps</dependency>
    </soy-resource>


    <web-item key="repository-settings-plugin-tab" name="Repository settings navigation tab"
              section="stash.repository.settings.panel/repository-settings-section" weight="50">
        <label>Work in progress...</label>
        <link>/plugins/servlet/example/repository/${repository.project.key}/${repository.slug}/settings</link>
        <tooltip>Change the repository portfolio setting</tooltip>
    </web-item>
</atlassian-plugin>


1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Tyler B [Atlassian]
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 16, 2014

Yes!

{call random.me.moreFields /}

Use 'call' just like you would for an 'in file template', but reference the namespace and template you want to use.

Josh Wheeler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 16, 2014

Thank you for the quick response. I actually had already tried that but unfortunately I got the following error:

[INFO] [talledLocalContainer] 2014-07-16 17:14:08,438 ERROR [http-bio-7990-exec-8] 1034x57x0 15nnamf 10.109.85.194 "GET /mvc/error500 HTTP/1.1" c.a.s.i.web.ErrorPageController There was an unhandled exception loading [/stash/plugins/servlet/example/repository/PROJECT_1/rep_1/settings]
[INFO] [talledLocalContainer] com.google.template.soy.tofu.SoyTofuException: In template plugin.example.anthill: Attempting to render undefined template 'random.me.moreFields'.

I'm not sure if I'm not calling some AUI components properly but this is what I'm currently implementing (I removed the HTML markup so it would fit):

templates.soy:

{namespace plugin.example}

/**
 * Template for the build
 * @param repository Repository object
 */
{template .anthill}
<html>
	<body>
		<h2>My Form</h2>
		<br />
		{call .form /}
		{call random.me.moreFields /}  // <-- Offending line
	</body>
</html>
{/template}

/**
 * Form info
 */
{template .form}
	{call aui.form.form}
		{param action: '' /}
		{param content}
			{call aui.form.textField}
		        {param id: 'projectName' /}
		        {param name: 'projectName' /}
		        {param type: 'text long-field' /}
		        {param labelContent: 'Anthill Project Name' /}
		    {/call}
			<br />
			{call aui.form.submit}
		  		{param type: 'submit' /}
		  		{param text: 'Save' /}
		  		{param id: 'portfolio.submit' /}
		  		{param label: 'Submit the portolio changes' /}
		  		{param value: 'Save' /}
		    {/call}
		{/param}
	{/call}
{/template}

selectField.soy:

{namespace random.me}

/**
 *	Testing more soy files
 */
{template .moreFields}
	{call aui.form.textField}
		{param id: 'textField1' /}
		{param name: 'textField1' /}
		{param type: 'text long-field' /}
		{param labelContent: 'textField1' /}
	{/call}
{/template}

Is it possible that I can't call it the way I am? Any suggestions would be very much appreciated.

Tyler B [Atlassian]
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 16, 2014

What does your atlassian-plugin.xml look like? Perhaps if you have not added your soy files as a resource, then your plugin will not no where to look for 'random.me.moreFields'

Something like this should be in your atlassian-plugin.xml:

<web-resource key="example-resources" name="Example Resources">
     <transformation extension="soy">
          <transformer key="soyTransformer"/>
     </transformation>
     <resource type="download" name="selectField-soy.js" location="/soy/selectField.soy"/>
</web-resource>

Adam
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 16, 2014

You also have to depend on that resource in the resource where your initial Soy file is - when we compile Soy resources we piggyback on the web-resource dpeendency system to find all the templates it depends on and should be compiled with.

Josh Wheeler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 16, 2014

@Tyler- I added the atlassian-plugin.xml file as requested.

@Tyler@Adam- I'm very new to plugin development and I don't understand how the resources and dependencies work. I've spent several days looking at all the documentation I can find and what I've posted is mostly copying and pasting from other sources. If you could provide some insight as to what is going on and/or resources where I can learn more it would be much appreciated.

Tyler B [Atlassian]
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 16, 2014

@JoshWheeler - checkout the web resource plugin module here: https://developer.atlassian.com/display/JIRADEV/Web+Resource+Plugin+Module. This could also warrant another question on answers.

You can delete the "Example Resources" web-resource, I see you have a "AUI Soy Templates" soy-resouce that does the job. Also, having both of your resources in the same resource allows them to depend on each other without having a specific <depedency> tag.

I think you need to double check your paths in "AUI Soy Templates":

&lt;resource type="download" name="atlassian.soy.js" location="soy/atlassian/atlassian.soy"/&gt;
&lt;resource type="download" name="form.soy.js"      location="soy/atlassian/form.soy"/&gt;

You said you had a selectForm.soy, not a form.soy.

Also, I believe soy-resource is deprecated and should be replaced with <web-resource>

Josh Wheeler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 17, 2014

That fixed it! I changed the <soy-resource> section to <web-resource> and modified the file path names. From what I gather, the format of a resource name of a soy file is "<soy file name>.soy.js". For benefit of others who may reference this page, this is what is in the atlassian-plugin.xml file:

&lt;web-resource key="aui-experimental-soy-templates" name="AUI Soy Templates"&gt;
        &lt;transformation extension="soy"&gt;
            &lt;transformer key="soyTransformer" /&gt;
        &lt;/transformation&gt;
        &lt;resource type="download" name="templates.soy.js" location="/templates/templates.soy"/&gt;
        &lt;resource type="download" name="selectField.soy.js" location="/templates/selectField.soy"/&gt;
        &lt;dependency&gt;com.atlassian.auiplugin:ajs&lt;/dependency&gt;
        &lt;dependency&gt;com.atlassian.soy.soy-template-plugin:soy-deps&lt;/dependency&gt;
    &lt;/web-resource&gt;

Thank you so much for your time and support!

TAGS
AUG Leaders

Atlassian Community Events