/// <summary>
 /// To write out the value of a condition expr, we invoke the evaluator 
 /// in eval.g to walk the condition tree computing the boolean value.
 /// If result is true, then we write subtemplate.
 /// </summary>
 public override int Write(StringTemplate self, IStringTemplateWriter output)
 {
     if (exprTree == null || self == null || output == null)
     {
         return 0;
     }
     // System.out.println("evaluating conditional tree: "+exprTree.toStringList());
     ActionEvaluator eval = new ActionEvaluator(self, this, output);
     ActionParser.initializeASTFactory(eval.getASTFactory());
     int n = 0;
     try
     {
         // get conditional from tree and compute result
         AST cond = exprTree.getFirstChild();
         bool includeSubtemplate = eval.ifCondition(cond); // eval and write out tree
         // System.out.println("subtemplate "+subtemplate);
         if (includeSubtemplate)
         {
             /* To evaluate the IF chunk, make a new instance whose enclosingInstance
             * points at 'self' so get attribute works.  Otherwise, enclosingInstance
             * points at the template used to make the precompiled code.  We need a
             * new template instance every time we exec this chunk to get the new
             * "enclosing instance" pointer.
             */
             StringTemplate s = subtemplate.GetInstanceOf();
             s.EnclosingInstance = self;
             // make sure we evaluate in context of enclosing template's
             // group so polymorphism works. :)
             s.Group = self.Group;
             s.NativeGroup = self.NativeGroup;
             n = s.Write(output);
         }
         else if (elseSubtemplate != null)
         {
             // evaluate ELSE clause if present and IF condition failed
             StringTemplate s = elseSubtemplate.GetInstanceOf();
             s.EnclosingInstance = self;
             s.Group = self.Group;
             s.NativeGroup = self.NativeGroup;
             n = s.Write(output);
         }
     }
     catch (RecognitionException re)
     {
         self.Error("can't evaluate tree: " + exprTree.ToStringList(), re);
     }
     return n;
 }
Exemple #2
0
        override public int Write(IStringTemplateWriter output)
        {
            SetPredefinedAttributes();
            SetDefaultArgumentValues();

            //IRailsEngineContext context = (IRailsEngineContext) GetAttribute(ConfigConstants.CONTEXT_ATTRIB_KEY);
            IRailsEngineContext context = MonoRailHttpHandler.CurrentContext;

            StringWriter writer = new StringWriter();
            StringTemplateViewContextAdapter viewComponentContext = new StringTemplateViewContextAdapter(viewComponentName, this);

            viewComponentContext.TextWriter = writer;


            viewComponent.Init(context, viewComponentContext);
            viewComponent.Render();
            if (viewComponentContext.ViewToRender != null)
            {
                StringTemplate viewST = group.GetEmbeddedInstanceOf(this, viewComponentContext.ViewToRender);
                writer.Write(viewST.ToString());
            }

            if (viewComponentName.Equals("CaptureFor"))
            {
                string keyToSet = (string)GetAttribute("id");
                object valToSet = GetAttribute(keyToSet);
                OutermostEnclosingInstance.RemoveAttribute(keyToSet);
                OutermostEnclosingInstance.SetAttribute(keyToSet, valToSet);
            }

            output.Write(writer.ToString());
            //			if (LintMode)
            //			{
            //				CheckForTrouble();
            //			}
            return(0);
        }
		override public int Write(IStringTemplateWriter output)
		{
			SetPredefinedAttributes();
			SetDefaultArgumentValues();

			//IRailsEngineContext context = (IRailsEngineContext) GetAttribute(ConfigConstants.CONTEXT_ATTRIB_KEY);
			IRailsEngineContext context = MonoRailHttpHandler.CurrentContext;

			StringWriter writer = new StringWriter();
			StringTemplateViewContextAdapter viewComponentContext = new StringTemplateViewContextAdapter(viewComponentName, this);
			viewComponentContext.TextWriter = writer;


			viewComponent.Init(context, viewComponentContext);
			viewComponent.Render();
			if (viewComponentContext.ViewToRender != null)
			{
				StringTemplate viewST = group.GetEmbeddedInstanceOf(this, viewComponentContext.ViewToRender);
				writer.Write(viewST.ToString());
			}

			if (viewComponentName.Equals("CaptureFor"))
			{
				string keyToSet = (string) GetAttribute("id");
				object valToSet = GetAttribute(keyToSet);
				OutermostEnclosingInstance.RemoveAttribute(keyToSet);
				OutermostEnclosingInstance.SetAttribute(keyToSet, valToSet);
			}

			output.Write(writer.ToString());
			//			if (LintMode)
			//			{
			//				CheckForTrouble();
			//			}
			return 0;
		}
Exemple #4
0
 /// <summary>
 /// How to write this node to output; return how many char written 
 /// </summary>
 /// <returns>Count of characters written.</returns>
 public abstract int Write(StringTemplate self, IStringTemplateWriter output);