Skip to content
kishore edited this page Jun 24, 2014 · 17 revisions

Like most templating engines, Slate templates consists of 3-4 major components/phases. these include the templates themselves and the features supported. The syntax for the tags, and how to supply data to the template.

  1. basics
  2. tags
  3. templates
  4. styles
  5. data
  6. limitations

license

Use cases

Listed below are some of the most common use cases for templates and how to execute them for android in java.

Prerequisites

slate template default settings rely on a few simple setups:

Refer to styles.xml in the Sample application in the the slate_beta.zip file

  1. having styles defined for H1 to H6, Text1 to Text6 in your styles
  2. having all your templates reside in the assets folder ( for now )
  3. having a default theme/styles file ( a sample is provided in the sample app )

1. Setup

TemplateService svc = new TemplateService();

// 1. give the templating the activity
// This is the easiest way to setup the context,
// resources, windows manager on the templating system.
svc.initialize((Activity)this);

// 2. Now set the resource strings / images
svc.Context.ResourceStyles = R.style.class;
svc.Context.ResourceStrings = R.string.class;
svc.Context.ResourceImages = R.drawable.class;

// 3. Load the theme file ( just like css )
svc.registerDefaultStyles();
svc.setThemeVariable("textColorAccent", "#2980B9", true);
svc.loadThemeFile("slate_theme_default.txt");

// 4. Put some data into template
svc.setData("firstName", "john doe");    	

// 4. Now execute template (must be located in assets folder )
TemplateView tview = svc.executeTemplateFile("slate_sample1.txt");
page.addView(R.id.pnlContainer, tview.RootView);

2: Load data

// Same setup as before..

// You can programmatically supply the data to the template.
// 1. Put in simple data values.
svc.setData("name", "slate");
svc.setData("isActive", false);
svc.setData("total", 304);
svc.setData("activeDate", new Date(2014,4,2));

// 2. Put in a hashmap data value named "weather"
HashMap<String, Object> weatherData = new HashMap<String, Object>();
weatherData.put("todayHigh", 60);
weatherData.put("todayLow", 40);
weatherData.put("todayConditions", "Sunny");
svc.setData("weather", weatherdata);

// 3. In the template you can then refer to the data 
// text6 @weather.todayHigh
// text6 @weather.todayLow
// text6 @weather.todayConditions

// 4. You can also put a custom object
// NOTE: The class must implement IValueStore ( refer to examples )
// This is allow retrieval of either Fields and/or properties.
// This allows you to also control the naming convention e.g. camel case.
User user = new User("john", "doe", 30);
svc.setData("user", user);

// Now you can get the custom objects properties or fields.
// text6 @user.firstName
// text6 @user.age

// 5. You can also put in lists of data
List<User> users = new ArrayList<User>();
users.add(new User("john", "doe", 30));
users.add(new User("john", "doe", 30));
svc.setData("users", users);

3: Load theme variables

svc.setThemeVariable("textColorAccent", "#2980B9", true);
svc.setThemeVariable("textColorContrast", "#333333", true);

4: Load template and get the generated control

TemplateView tview = svc.executeTemplateFile("slate_sample1.txt");
View view = tview.RootView;

5: Load template and get the generated controls by name

TemplateView tView = svc.executeTemplateFile("slate_sample1.txt");

// 1. Get control by name ( if name was setup in template )
View view = tview.getViewByName("firstName");

// 2. Controls get ids ( incrementing integers ), get id of view
int id = tview.getViewId("firstName");

// 3. With the id, you can also retrieve the view by id
View view = tview.getViewById(id);

6: Register click handler

TemplateView tView = svc.executeTemplate(templateContent);
view.setOnClickListener("firstName", this);

...
@Override
public void onClick(View view) {
	if(view.getTag().toString().equals("action_phone"))
	{
		_templateView.setText("message", "phone icon clicked", null);
	}
	else if(view.getTag().toString().equals("action_chat"))
	{
		_templateView.setText("message", "chat icon clicked", null);
	}
	else if(view.getTag().toString().equals("action_share"))
	{
		_templateView.setText("message", "share icon clicked", null);
	}
}

Redmi Note 7

(Showing 1 – 6 products of 6 products)
Sort By
Popularity
Price -- Low to High
Price -- High to Low
Newest First
Delivering to
Change
No products available at the current pincode
Clone this wiki locally