Meteor - 将模板变量作为内部模板参数传递

时间:2016-10-29 03:50:55

标签: templates meteor

我有问题。我正在尝试将Spacebars HTML中的Template变量作为参数传递给子模板:

public class SWN3 {
private String pathToSWN = "data"+File.separator+"SentiWordNet_3.0.0.txt";
private HashMap<String, String> _dict;

public SWN3(){

    _dict = new HashMap<String, String>();
    HashMap<String, Vector<Double>> _temp = new HashMap<String,     Vector<Double>>();
    try{
        BufferedReader csv =  new BufferedReader(new FileReader(pathToSWN));
        String line = "";           
        while((line = csv.readLine()) != null)
        {
            String[] data = line.split("\t");
            Double score = Double.parseDouble(data[2])-      Double.parseDouble(data[3]);
            String[] words = data[4].split(" ");
            for(String w:words)
            {
                String[] w_n = w.split("#");
                w_n[0] += "#"+data[0];
                int index = Integer.parseInt(w_n[1])-1;
                if(_temp.containsKey(w_n[0]))
                {
                    Vector<Double> v = _temp.get(w_n[0]);
                    if(index>v.size())
                        for(int i = v.size();i<index; i++)
                            v.add(0.0);
                    v.add(index, score);
                    _temp.put(w_n[0], v);
                }
                else
                {
                    Vector<Double> v = new Vector<Double>();
                    for(int i = 0;i<index; i++)
                        v.add(0.0);
                    v.add(index, score);
                    _temp.put(w_n[0], v);
                }
            }
        }
        Set<String> temp = _temp.keySet();
        for (Iterator<String> iterator = temp.iterator(); iterator.hasNext();)      {
            String word = (String) iterator.next();
            Vector<Double> v = _temp.get(word);
            double score = 0.0;
            double sum = 0.0;
            for(int i = 0; i < v.size(); i++)
                score += ((double)1/(double)(i+1))*v.get(i);
            for(int i = 1; i<=v.size(); i++)
                sum += (double)1/(double)i;
            score /= sum;
            String sent = "";               
            if(score>=0.75)
                sent = "strong_positive";
            else
            if(score > 0.25 && score<=0.5)
                sent = "positive";
            else
            if(score > 0 && score>=0.25)
                sent = "weak_positive";
            else
            if(score < 0 && score>=-0.25)
                sent = "weak_negative";
            else
            if(score < -0.25 && score>=-0.5)
                sent = "negative";
            else
            if(score<=-0.75)
                sent = "strong_negative";
            _dict.put(word, sent);
        }
    }
    catch(Exception e){e.printStackTrace();}        
}

public String extract(String word, String pos)
{
    return _dict.get(word+"#"+pos);
}

通常,上面的语法适用于在HTML元素中传递变量,但它只是在输出HTML中显示为文字字符串'hello {{var1})'

是否可以将模板变量作为参数传递给子模板/如何?

1 个答案:

答案 0 :(得分:0)

您可以简单地使用{{> childTemp val=var1}}语法,假设您首先执行以下操作:

Template.example.onCreated(function () {
  this.var1 = "whatever";
});

Template.example.helpers({
  var1 () {
    return Template.instance().var1;
  }
});