Hello,
i want to integrate a javascript-file (via the <script scr="...."> </script> tag)
into a soy-template but i have no clue how to do this.
{namespace plugin.profile} /** * @param user StashUser object which is in the context provided by ProfileServlet */ {template .profileTab} <html> <head> <meta name="decorator" content="stash.users.profile"> <meta name="userSlug" content="{$user.slug}"> <meta name="activeTab" content="profile-plugin-tab"> <title>{$user.displayName} / Profile Tab</title> <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script scr=" the path to the javascript-file "></script>
</head> <body> Hello World
<button type="button" id="clickme">Click Me!</button> </html> {/template}
$(function() { alert('test'); $("#clickme").click(function() { alert('helloworld'); }); });
private SoyTemplateRenderer soyTemplateRenderer; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { . . . resp.setContentType("text/html;charset=UTF-8"); . . . try { render(resp, "plugin.profile.profileTab", map); } catch (Exception e) { log.error("An error occurred in HtmlServlet.render(...) ", e); } } private void render(HttpServletResponse resp, String templateName, Map<String, Object> data) throws Exception{ String arg1 = "de.neosit.stash.stash-all-commits:profile-soy"; soyTemplateRenderer.render(resp.getWriter(), arg1,templateName, data); }
<servlet name="Html Servlet" i18n-name-key="html-servlet.name" key="html-servlet" class="de.neosit.stash.servlet.HtmlServlet"> <description key="html-servlet.description">The Html Servlet Plugin</description> <url-pattern>/all-commits/*</url-pattern> </servlet> <web-item key="profile-plugin-tab" name="Profile navigation tab" section="stash.user.profile.secondary.tabs" weight="20"> <label key="profile.plugin.tab">My Plugin -1</label> <link>/plugins/servlet/all-commits/index</link> <tooltip key="profile.plugin.tab.description">Hooray, we have a tab!</tooltip> </web-item> <stash-resource key="profile-soy" name="Profile Tab Soy Templates"> <directory location="/templates/" /> </stash-resource
Thank you in advance,
Tobias
Community moderators have prevented the ability to post new answers.
Hi Tobias,
You don't need to manually link to your <script> tag via the Soy. When you use the Atlassian decorators, we automagically insert all the required javascript files based on a number of 'contexts'.
In the case above, if you're using the 'stash.users.profile' decorator, that will be using the 'stash.layout.user.profile' context. If you add <context>stash.layout.user.profile</context> to your <stash-resource> in the atlassian-plugin.xml you _should_ find that your scripts are included.
Alternatively you can make your own context by injecting com.atlassian.plugin.webresource.WebResourceManager into your Servlet and then calling this before the soyTemplateRenderer.render() method.
webResourceManager.requireResourcesForContext("plugin.my.context");
Does that help?
Charles
Thank you for your fast answer and it helps a lot :)
I decided to put my javascript-code into the default /js/stash-all-commits.js file and use the '<context>stash.layout.user.profile</context>' tag in my atlassian-plugin.xml :
<stash-resource key="javascript-code" name="JavaScript-Code">
<directory location="/js/" />
<context>stash.layout.user.profile</context>
</stash-resource>
... but an error occurred :(
My JavaScript-file has been loaded but it cant use jQuery-commands because jQuery has been loaded after my stash-all-commits.js file.
the sourcecode of my "plugin-page" localhost:7990/stash/plugins/servlet/all-commits/index
....
<link type="text/css" rel="stylesheet" href="/stash/s/d41d8cd98f00b204e9800998ecf8427e/de_DE-1988229788/be1a6cd/416/3.5.7/_/download/resources/com.atlassian.support.stp:stp-license-status-resources/stp-licenseStatus.css" media="all">
<script type="text/javascript" src="/stash/s/d41d8cd98f00b204e9800998ecf8427e/de_DE-1988229788/be1a6cd/416/1.0-SNAPSHOT/_/download/resources/de.neosit.stash.stash-all-commits:javascript/stash-all-commits.js" ></script>
<script type="text/javascript" src="/stash/s/d41d8cd98f00b204e9800998ecf8427e/de_DE-1988229788/be1a6cd/416/5.2-m5/_/download/resources/com.atlassian.auiplugin:jquery-lib/jquery.js" ></script>
<script type="text/javascript" src="/stash/s/d41d8cd98f00b204e9800998ecf8427e/de_DE-1988229788/be1a6cd/416/5.2-m5/_/download/resources/com.atlassian.auiplugin:jquery-compatibility/jquery-compatibility.js" ></script>
....
How can i fix it?
Thank you in advance,
Tobias
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Tobias,
You need to add a dependency so that our web manager can order the resources correctly. The follow global module contains most of the usual goodies like jQuery and Underscore:
<stash-resource key="..." >
....
<dependency>com.atlassian.stash.stash-web-plugin:global</dependency>
</stash-resource>
Cheers,
Charles
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.