当前位置: 首页>>代码示例>>Java>>正文


Java Template类代码示例

本文整理汇总了Java中org.wisdom.api.templates.Template的典型用法代码示例。如果您正苦于以下问题:Java Template类的具体用法?Java Template怎么用?Java Template使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Template类属于org.wisdom.api.templates包,在下文中一共展示了Template类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getReader

import org.wisdom.api.templates.Template; //导入依赖的package包/类
/**
 * Gets a {@link java.io.Reader} object on the source of the template having the given name. This method is used
 * to resolved partials.
 *
 * @param name the name of the template
 * @return a reader to read the template's source.
 * @throws com.github.mustachejava.MustacheException if the template cannot be found or read.
 */
@Override
public Reader getReader(String name) {
    // On windows the path containing '..' are not stripped from the path, so we ensure they are.
    String simplified = name;
    if (name.contains("..")) {
        simplified = Files.simplifyPath(name);
    }
    // Take into account absolute path
    if (simplified.startsWith("/") && simplified.length() > 1) {
        simplified = simplified.substring(1, simplified.length());
    }
    for (Template t : collector.getTemplates()) {
        MustacheTemplate template = (MustacheTemplate) t;
        if (template.name().equals(simplified)) {
            try {
                return IOUtils.toBufferedReader(new StringReader(IOUtils.toString(template.getURL())));
            } catch (IOException e) {
                throw new MustacheException("Cannot read the template " + name, e);
            }
        }
    }

    throw new MustacheException("Template \'" + name + "\' not found");
}
 
开发者ID:wisdom-framework,项目名称:wisdom-mustache-template-engine,代码行数:33,代码来源:ExtendedMustacheFactory.java

示例2: manageTemplates

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void manageTemplates() throws Exception {
    BundleContext ctxt = mock(BundleContext.class);
    when(ctxt.registerService(any(Class.class), any(Template.class), any(Dictionary.class))).thenReturn(mock
            (ServiceRegistration.class));
    MustacheTemplateCollector collector = new MustacheTemplateCollector(ctxt);

    assertThat(collector.getTemplates()).isEmpty();
    File javascript = new File("src/test/resources/templates/kitten1.mst");
    collector.addTemplate(javascript.toURI().toURL());

    assertThat(collector.getTemplates()).hasSize(1);

    collector.updatedTemplate(javascript);
    collector.deleteTemplate(javascript);

    assertThat(collector.getTemplates()).hasSize(0);

    collector.stop();
}
 
开发者ID:wisdom-framework,项目名称:wisdom-mustache-template-engine,代码行数:21,代码来源:MustacheTemplateCollectorTest.java

示例3: testJsonTemplate

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void testJsonTemplate() throws MalformedURLException {
    Template template = osgi.getServiceObject(Template.class, "(name=kitten2)");
    assertThat(template).isNotNull();
    assertThat(template.engine()).isEqualTo("mustache");
    assertThat(template.mimetype()).isEqualTo(MimeTypes.JSON);
    assertThat(template.name()).isEqualTo("kitten2");

    Renderable renderable = template.render(new DefaultController() {
    }, ImmutableMap.<String, Object>of("items", Cat.cats()));

    assertThat(renderable.mimetype()).isEqualTo(MimeTypes.JSON);
    assertThat((String) renderable.content())
            .contains("\"name\": \"romeo\",")
            .contains("\"name\": \"tom\",");
}
 
开发者ID:wisdom-framework,项目名称:wisdom-mustache-template-engine,代码行数:17,代码来源:MustacheInContainerIT.java

示例4: testTemplateUsingAFunction

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void testTemplateUsingAFunction() throws MalformedURLException {
    Template template = osgi.getServiceObject(Template.class, "(name=function/function)");
    assertThat(template.engine()).isEqualTo("mustache");
    assertThat(template.mimetype()).isEqualTo(MimeTypes.TEXT);
    assertThat(template.name()).isEqualTo("function/function");

    Renderable renderable = template.render(new DefaultController() {
    }, ImmutableMap.of(
            "name", "Wisdom",
            "wrapped", new TemplateFunction() {
                @Nullable
                @Override
                public String apply(String input) {
                    return "<b>" + input + "</b>";
                }
            }

    ));

    assertThat(renderable.mimetype()).isEqualTo(MimeTypes.TEXT);
    assertThat((String) renderable.content()).contains("<b>").contains("Wisdom is awesome.").contains("</b>");
}
 
开发者ID:wisdom-framework,项目名称:wisdom-mustache-template-engine,代码行数:24,代码来源:MustacheInContainerIT.java

示例5: testMailTemplate

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void testMailTemplate() throws MalformedURLException {
    Template template = osgi.getServiceObject(Template.class, "(name=mustache/mail)");
    assertThat(template.engine()).isEqualTo("mustache");
    assertThat(template.mimetype()).isEqualTo(MimeTypes.TEXT);
    assertThat(template.name()).isEqualTo("mustache/mail");

    Renderable renderable = template.render(new DefaultController() {
    }, ImmutableMap.<String, Object>of(
            "name", "Wisdom",
            "value", 10000,
            "taxed_value", 10000 - (10000 * 0.4),
            "in_ca", true
    ));

    assertThat(renderable.mimetype()).isEqualTo(MimeTypes.TEXT);
    assertThat((String) renderable.content())
            .contains("Hello Wisdom")
            .contains("You have just won 10000 dollars!")
            .contains("Well, 6000.0 dollars, after taxes.");
}
 
开发者ID:wisdom-framework,项目名称:wisdom-mustache-template-engine,代码行数:22,代码来源:MustacheInContainerIT.java

示例6: testSectionAndInvertedSection

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void testSectionAndInvertedSection() throws MalformedURLException {
    Template template = osgi.getServiceObject(Template.class, "(name=mustache/section)");

    Renderable renderable = template.render(new DefaultController() {
    }, ImmutableMap.<String, Object>of(
            "repo", ImmutableList.of(
                    ImmutableMap.of("name", "central"),
                    ImmutableMap.of("name", "local"))
    ));

    assertThat(renderable.mimetype()).isEqualTo(MimeTypes.TEXT);
    assertThat((String) renderable.content())
            .contains("<b>central</b>")
            .contains("<b>local</b>");

    renderable = template.render(new DefaultController() {
    }, ImmutableMap.<String, Object>of(
            "repo", ImmutableList.of()
    ));

    assertThat((String) renderable.content())
            .contains("No repos :(");
}
 
开发者ID:wisdom-framework,项目名称:wisdom-mustache-template-engine,代码行数:25,代码来源:MustacheInContainerIT.java

示例7: testPartials

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void testPartials() throws MalformedURLException {
    Template template = osgi.getServiceObject(Template.class, "(name=mustache/base)");
    assertThat(template.engine()).isEqualTo("mustache");
    assertThat(template.mimetype()).isEqualTo(MimeTypes.HTML);
    assertThat(template.name()).isEqualTo("mustache/base");

    Renderable renderable = template.render(new DefaultController() {
    }, ImmutableMap.<String, Object>of(
            "names", ImmutableList.of(
                    ImmutableMap.of("name", "romeo"),
                    ImmutableMap.of("name", "gros minet"),
                    ImmutableMap.of("name", "tom")
            )
    ));

    assertThat(renderable.mimetype()).isEqualTo(MimeTypes.HTML);
    assertThat((String) renderable.content())
            .contains("<strong>romeo</strong")
            .contains("<strong>gros minet</strong>")
            .contains("<strong>tom</strong>");
}
 
开发者ID:wisdom-framework,项目名称:wisdom-mustache-template-engine,代码行数:23,代码来源:MustacheInContainerIT.java

示例8: testPartialsUsingAbsolutePath

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void testPartialsUsingAbsolutePath() throws MalformedURLException {
    Template template = osgi.getServiceObject(Template.class, "(name=mustache/baseUsingAbsolutePath)");
    assertThat(template.engine()).isEqualTo("mustache");
    assertThat(template.mimetype()).isEqualTo(MimeTypes.HTML);
    assertThat(template.name()).isEqualTo("mustache/baseUsingAbsolutePath");

    Renderable renderable = template.render(new DefaultController() {
    }, ImmutableMap.<String, Object>of(
            "names", ImmutableList.of(
                    ImmutableMap.of("name", "romeo"),
                    ImmutableMap.of("name", "gros minet"),
                    ImmutableMap.of("name", "tom")
            )
    ));

    assertThat(renderable.mimetype()).isEqualTo(MimeTypes.HTML);
    assertThat((String) renderable.content())
            .contains("<strong>romeo</strong")
            .contains("<strong>gros minet</strong>")
            .contains("<strong>tom</strong>");
}
 
开发者ID:wisdom-framework,项目名称:wisdom-mustache-template-engine,代码行数:23,代码来源:MustacheInContainerIT.java

示例9: testInheritance

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void testInheritance() throws MalformedURLException {
    Template template = osgi.getServiceObject(Template.class, "(name=inheritance/home/welcome)");
    assertThat(template.engine()).isEqualTo("mustache");
    assertThat(template.mimetype()).isEqualTo(MimeTypes.HTML);

    Renderable renderable = template.render(new DefaultController() {
                                            }, ImmutableMap.<String, Object>of(
                    "title", "Hello Wisdom", "name", "you")
    );

    assertThat(renderable.mimetype()).isEqualTo(MimeTypes.HTML);
    assertThat((String) renderable.content())
            .contains("<title>Welcome page</title>")
            .contains("<p>Content from base</p>")
            .contains("<h1>Hello you,");
}
 
开发者ID:wisdom-framework,项目名称:wisdom-mustache-template-engine,代码行数:18,代码来源:MustacheInContainerIT.java

示例10: render

import org.wisdom.api.templates.Template; //导入依赖的package包/类
/**
 * Renders the given template.
 *
 * @param template   the template
 * @param parameters the parameters given as list following the scheme: key, value, key, value...
 * @return the renderable object.
 */
public Renderable<?> render(Template template, Object... parameters) {
    Map<String, Object> map = Maps.newHashMap();
    String key = null;
    for (Object parameter : parameters) {
        if (key == null) {
            if (!(parameter instanceof String)) {
                throw new IllegalArgumentException("The template variable name " + parameter + " must be a string");
            } else {
                key = (String) parameter;
            }
        } else {
            map.put(key, parameter);
            key = null;
        }
    }
    if (key != null) {
        throw new IllegalArgumentException("Illegal number of parameter, the variable " + key + " has no value");
    }
    return template.render(this, map);
}
 
开发者ID:wisdom-framework,项目名称:wisdom,代码行数:28,代码来源:DefaultController.java

示例11: visitEnd

import org.wisdom.api.templates.Template; //导入依赖的package包/类
/**
 * Generates the element-attribute structure.
 */
@Override
public void visitEnd() {
    if (name == null  || name.length() == 0) {
        reporter.error("The 'name' attribute of @View from " + workbench.getType().getClassName() + " must be " +
                "set");
        return;
    }

    // Check the type of the field
    if (! Type.getDescriptor(Template.class).equals(node.desc)) {
        reporter.error("The type of the field " + field + " from " + workbench.getType().getClassName() + " must " +
                "be " + Template.class.getName() + " because the field is annotated with @View");
    }

    Element requires = new Element("requires", "");
    requires.addAttribute(new Attribute("field", field));
    requires.addAttribute(new Attribute("filter", getFilter(name)));

    workbench.getElements().put(requires, null);
}
 
开发者ID:wisdom-framework,项目名称:wisdom,代码行数:24,代码来源:WisdomViewVisitor.java

示例12: parseRegularView

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void parseRegularView() {
    Reporter reporter = new SystemReporter();
    ComponentWorkbench workbench = mock(ComponentWorkbench.class);
    when(workbench.getType()).thenReturn(Type.getType(MyComponent.class));
    when(workbench.getClassNode()).thenReturn(new ClassNode());
    when(workbench.getElements()).thenReturn(elements);

    FieldNode node = new FieldNode(Opcodes.ACC_PROTECTED, "template", Type.getDescriptor(Template.class), null, null);

    WisdomViewVisitor visitor = new WisdomViewVisitor(workbench, reporter, node);
    visitor.visit("value", "index");
    visitor.visitEnd();

    assertThat(elements).hasSize(1);
    Element element = elements.keySet().iterator().next();
    assertThat(element.getName()).isEqualTo("requires");
    assertThat(element.getAttribute("field")).isEqualTo("template");
    assertThat(element.getAttribute("filter")).contains("(name=index)");

}
 
开发者ID:wisdom-framework,项目名称:wisdom,代码行数:22,代码来源:WisdomViewVisitorTest.java

示例13: parseEmptyAnnotation

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void parseEmptyAnnotation() {
    Reporter reporter = new SystemReporter();
    ComponentWorkbench workbench = mock(ComponentWorkbench.class);
    when(workbench.getType()).thenReturn(Type.getType(MyComponent.class));
    when(workbench.getClassNode()).thenReturn(new ClassNode());
    when(workbench.getElements()).thenReturn(elements);

    FieldNode node = new FieldNode(Opcodes.ACC_PROTECTED, "template",
            Type.getDescriptor(Template.class), null, null);

    WisdomViewVisitor visitor = new WisdomViewVisitor(workbench, reporter, node);
    visitor.visitEnd();

    assertThat(elements).hasSize(0);
}
 
开发者ID:wisdom-framework,项目名称:wisdom,代码行数:17,代码来源:WisdomViewVisitorTest.java

示例14: addTemplate

import org.wisdom.api.templates.Template; //导入依赖的package包/类
/**
 * Adds a template form the given url.
 *
 * @param bundle the bundle containing the template, use system bundle for external templates.
 * @param templateURL the url
 * @return the added template. IF the given url is already used by another template, return this other template.
 */
public ThymeLeafTemplateImplementation addTemplate(Bundle bundle, URL templateURL) {
    ThymeLeafTemplateImplementation template = getTemplateByURL(templateURL);
    if (template != null) {
        // Already existing.
        return template;
    }
    synchronized (this) {
        // need to be synchronized because of the access to engine.
        template = new ThymeLeafTemplateImplementation(engine, templateURL,
                router, assets, bundle);
    }
    ServiceRegistration<Template> reg = context.registerService(Template.class, template,
            template.getServiceProperties());
    registrations.put(template, reg);
    LOGGER.debug("Thymeleaf template added for {}", templateURL.toExternalForm());
    return template;
}
 
开发者ID:wisdom-framework,项目名称:wisdom,代码行数:25,代码来源:ThymeleafTemplateCollector.java

示例15: testSessionScope

import org.wisdom.api.templates.Template; //导入依赖的package包/类
@Test
public void testSessionScope() {
    final WisdomTemplateEngine engine = createWisdomEngine();
    engine.initialize();
    final Template template = mock(Template.class);
    when(template.fullName()).thenReturn("templates/var.thl.html");

    final FakeRouter router = new FakeRouter();
    final Controller controller = new FakeController();
    router.addController(controller);
    final Assets assets = mock(Assets.class);


    Action.ActionResult result = action(new Invocation() {
        @Override
        public Result invoke() throws Throwable {
            return ok(engine.process(template, controller, router, assets, ImmutableMap.<String, Object>of("key",
                    "test")));
        }
    }).with(new FakeContext().addToSession("key2", "session")).invoke();

    String content = (String) result.getResult().getRenderable().content();
    assertThat(content)
            .contains("<span>KEY</span> = <span>test</span>")
            .contains("<span>KEY2</span> = <span>session</span>");
}
 
开发者ID:wisdom-framework,项目名称:wisdom,代码行数:27,代码来源:WisdomTemplateEngineTest.java


注:本文中的org.wisdom.api.templates.Template类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。