public ThemeDefinition getDefinition() { if (definition != null) { return definition; } if (parent != null) { return parent.getDefinition(); } return new ThemeDefinition(); }
import com.x5.template.Theme; import com.x5.template.Chunk; ... Theme theme = new Theme(); Chunk html = theme.makeChunk("my_template"); html.set("class_a", "blue_cell"); html.set("class_b", "plain_cell"); String row1 = "[[color,text],[blue,moe],[red,curly],[orange,larry]]"; String row2 = "[[color,text],[red,hat],[black,dog],[blue,bottle]]"; String[] list = new String[]{row1,row2}; html.set("list", list); out = getOutputWriter(); html.render( out ); // or System.out.print( html.toString() ) out.flush(); out.close();
private String getTemplateContent(String name, Optional<Breakpoint> breakpoint) throws TemplateNotFoundException, IOException { String result = getTemplateContent(getActiveTheme(), name, breakpoint); if (result == null) { throw new TemplateNotFoundException(); } return result; }
private Optional<AddonGroupDefinition> findGroupDefinition(String groupName) { // First, find it in platform (has priority) if (platformSettings.getAddons().containsKey(groupName)) { return Optional.of(platformSettings.getAddons().get(groupName)); } // Then, in theme if (context.getTheme() != null && context.getTheme().getDefinition() != null && context.getTheme().getDefinition().getAddons().containsKey(groupName)) { return Optional.of(context.getTheme().getDefinition().getAddons().get(groupName)); } return Optional.absent(); } }
private String generateTemplateId(String layoutName, Optional<Breakpoint> breakpoint) { String themeName = context.getTheme().getDefinition().getName(); String templateId = themeName.length() + themeName + "_" + breakpoint.toString().length() + breakpoint.toString() + "_" + layoutName.length() + layoutName; return "" + templateId.hashCode(); }
private ThemeDefinition getThemeDefinition() { if (context.getTheme() == null) { return null; } return context.getTheme().getDefinition(); }
Path path = theme.getPath(); if (breakpoint.isPresent()) { path = path.resolve(breakpoint.get().getFolder()); switch (theme.getType()) { case FILE_SYSTEM: if (path.toFile().exists()) { return new ThemeResource(ThemeResource.Type.FILE, path); try { Resources.getResource(path.toString()); return new ThemeResource(ThemeResource.Type.CLASSPATH_RESOURCE, path); } catch (IllegalArgumentException e) { return getResource(theme, name, Optional.<Breakpoint>absent()); if (theme.getParent() != null) { return getResource(theme.getParent(), name, breakpoint);
@Override public Template getTemplate(Theme theme, String name, Optional<Breakpoint> breakpoint) throws TemplateNotFoundException { try { String content = this.getTemplateContent(theme, name, breakpoint); Template template = new Template(generateTemplateId(name, breakpoint), content, true); return template; } catch (IOException e) { throw new TemplateNotFoundException(e); } }
/** * Tries to get template content for a certain theme and breakpoint. Tries in order : - tenant theme folder in * persistent directory (example : data/tenants/thetenant/themes/thetheme/[breakpoint/]filename - global theme * folder in persistent directory (example : data/themes/thetheme/[breakpoint/]filename) - classpath (example uri : * /themes/thetheme/[breakpoint/]filename) For each step, checks the breakpoint (if set) and fallback on the * no-breakpoint file * * @param theme the theme for which to try and get the template content * @param name the name of the template to get * @param breakpoint the breakpoint for which to get the template content * @return the content of the template, or null if not found * @throws IOException when there is an IO exception getting the content */ private String getTemplateContent(Theme theme, String name, Optional<Breakpoint> breakpoint) throws IOException { ThemeResource resource = this.getResource(theme, name, breakpoint); if (resource == null) { return null; } switch (resource.getType()) { default: case FILE: return Files.toString(resource.getPath().toFile(), Charsets.UTF_8); case CLASSPATH_RESOURCE: return Resources.toString(Resources.getResource(resource.getPath().toString()), Charsets.UTF_8); } }
@Override public Optional<String> resolveModelPath(String id) { if (getThemeDefinition() == null) { return Optional.absent(); } Map<String, Model> models = getThemeDefinition().getModels(); if (models.containsKey(id)) { return Optional.fromNullable(models.get(id).getFile()); } return Optional.absent(); }
@Override public Template getGlobalTemplate(String name, Optional<Breakpoint> breakpoint) throws TemplateNotFoundException { try { return new Template(generateTemplateId(name, breakpoint), Resources.toString(Resources.getResource("templates/" + name), Charsets.UTF_8), true); } catch (IOException e) { throw new TemplateNotFoundException(); } }
public ImageContext createPlaceholderImageContext(boolean featured) { ImageContext context = new ImageContext("http://placehold.it/800x800"); if (theme != null && theme.getImageFormats().size() > 0) { for (String dimensionName : theme.getImageFormats().keySet()) { // Note: if only one dimension is passed for an image format (it means the image format is supposed // to respect the original image aspect ratio according to the one dimension passed), we present the // placeholder image as a square. ImageFormatDefinition definition = theme.getImageFormats().get(dimensionName); String url = MessageFormat.format("http://placehold.it/{0,number,#}x{1,number,#}", definition.getWidth() != null ? definition.getWidth() : definition.getHeight(), definition.getHeight() != null ? definition.getHeight() : definition.getWidth()); context.put("theme_" + dimensionName + "_" + ContextConstants.URL, url); } } return context; }
@Override public Breakpoint getValue(HttpContext httpContext) { return this.breakpointDetector.getBreakpoint(httpContext.getRequest().getHeaderValue("User-Agent")).orNull(); } }
public PaginationDefinition getPaginationDefinition(String key) { return pagination.containsKey(key) ? pagination.get(key) : new PaginationDefinition(); }
private void addModels(Map<String, Map<String, Object>> entities, Map<String, Model> models) { // Step 1 : add models defined explicitly for some entities for (String modelId : models.keySet()) { Model model = models.get(modelId); if (model.getEntities().isPresent()) { for (String entity : model.getEntities().get()) { addModelsToEntity(entities, entity, modelId, model); } } } // Step 2 add addon groups for all entities for (String modelId : models.keySet()) { Model model = models.get(modelId); if (!model.getEntities().isPresent()) { for (String entity : entities.keySet()) { addModelsToEntity(entities, entity, modelId, model); } } } }
@Override public Template getIndexTemplate(Optional<Breakpoint> breakpoint) throws TemplateNotFoundException { try { String content = this.getTemplateContent(INDEX_HTML, breakpoint); Template template = new Template(generateTemplateId(INDEX_HTML, breakpoint), content); return template; } catch (IOException e) { throw new TemplateNotFoundException(e); } }
@Override public Template getTemplate(String name, Optional<Breakpoint> breakpoint) throws TemplateNotFoundException { try { String content = this.getTemplateContent(name, breakpoint); Template template = new Template(generateTemplateId(name, breakpoint), content, true); return template; } catch (IOException e) { throw new TemplateNotFoundException(e); } }
import com.x5.template.Theme; import com.x5.template.Chunk; import java.io.File; import java.io.FileWriter; import java.io.IOException; ... public void writeTemplatedFile() throws IOException { Theme theme = new Theme(); Chunk chunk = theme.makeChunk("my_template","txt"); // replace static values below with user input chunk.set("name", "Lancelot"); chunk.set("favorite_color", "blue"); String outfilePath = getFilePath(); File file = new File(outfilePath); FileWriter out = new FileWriter(file); chunk.render( out ); out.flush(); out.close(); }