0

I am trying to integrate Minitemplator (http://www.source-code.biz/MiniTemplator/) to an application of android but i am little lost.

I can access to the template file, the template file is in asset directory and i try to get the file at this way:

Uri path = Uri.parse("file:///android_asset/index.html");

and instantiate the object at this way:

MiniTemplator t = new MiniTemplator(path.getPath());

but it send me an io exception that file or folder doesn't exists.

how is the correct way to send the file to instantiate my minitemplator object?

This is the complete code:

package com.kentverger.minitemplator;

import java.io.File;
import java.io.IOException;

import biz.source_code.miniTemplator.MiniTemplator;
import biz.source_code.miniTemplator.MiniTemplator.TemplateSyntaxException;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebView;

public class Template extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_template);

        WebView index = (WebView) findViewById(R.id.indexWebView);

        Uri path = Uri.parse("file:///android_asset/index.html");
        try {
            MiniTemplator t = new MiniTemplator(path.getPath());

            t.setVariable("titulo", "Hola mundo generado desde java");

            String html_code = t.generateOutput();

            index.loadData(html_code, "text/html", null);

        } catch (TemplateSyntaxException e) {
            Log.d("ERROR 1", e.getMessage());
        } catch (IOException e) {
            Log.d("ERROR 2", e.getMessage());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_template, menu);
        return true;
    }
}
Piyush
  • 2,589
  • 6
  • 38
  • 77
kentverger
  • 475
  • 1
  • 5
  • 19

2 Answers2

2

I solve my problem loading the template from a string instead a html file.

First I have to create a specification object that will content the template String

MiniTemplator.TemplateSpecification templateSpec = new MiniTemplator.TemplateSpecification();

Then add the template String to templateString property

templateSpec.templateText = "<html><body><h1>${hello}</h1></body></html>";

Next we have to instantiate the Minitemplator object with the speciciations object

t = new MiniTemplator(templateSpec);

And we already to change the values of the variables in the template like this:

t.setVariable("hello", "Hola Mundo!");

Thanks a lot :)

kentverger
  • 475
  • 1
  • 5
  • 19
0
 MiniTemplator miniTemplator = openHtmlFileFromAssert(activity, "test.html");

Try this method is working and is help to read html file from assert folder

 public MiniTemplator openHtmlFileFromAssert(Activity activity, String fileName) {

    try {
        AssetManager assetManager = activity.getAssets();
        InputStream inputStream = assetManager.open(fileName);
        MiniTemplator.Builder templateBuilder = new MiniTemplator.Builder();
        return templateBuilder.build(inputStream, Charset.defaultCharset());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
Jey Arun
  • 103
  • 5