Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 945815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:48:45+00:00 2026-05-15T22:48:45+00:00

I am trying to write a simple template engine to parse if statements. <{if

  • 0

I am trying to write a simple template engine to parse if statements.

<{if $value == true}>
    say hello
<{/if}>

I have written working code However, If I have multiple if statements in the file I am parsing then if grabs only the first and last if statement. For example.

<{if $value1 == true}>
    say hello
<{/if}>
...
<{if $value2 == true}>
    say hello
<{/if}>
...

The code parses and returns:

    say hello
<{/if}>
...
<{if $value2 == true}>
    say hello

Rather than just returning say hello.

Thanks for your help In advance the code is as follows:

    public class Templates {

        private static String escape(String value) {
            return value.replaceAll("\\$", "\\\\\\$");
        }

        public static String load(String name) {
            return load(name, null);
        }

        public static String load(String name, Map<String, String> parse) {

            String page = new Resources().getTextResource("lib/tpl/" + name);

            if (page == null) {
                return "The template, " + name + " was NOT FOUND.";
            }

            if (parse != null) {
                Iterator it = parse.entrySet().iterator();

                while (it.hasNext()) {

                    Map.Entry entry = (Map.Entry) it.next();
                    String key = (String) entry.getKey();
                    String value = (String) entry.getValue();

                    value = escape(value); // Prevents java exception. Can't deal with $

                    page = page.replaceAll("\\<\\{\\$" + key + "\\}\\>", value);


                }

                Pattern ptrn = Pattern.compile("\\<\\{if \\$([a-z]+)\\s*(==|!=|eq|neq|or|and)\\s*(\\w+)\\}\\>(\\p{ASCII}+)\\<\\{/if\\}\\>");

                Matcher mtch = ptrn.matcher(page);

                System.out.println("\n\n\n");

                while(mtch.find()) {

                    System.out.println("Key is: " + mtch.group(1));
                    //System.out.println("Key: " + dateMatcher.group(2));
                    System.out.println("Value is: " + mtch.group(3));
                    System.out.println("Data is: " + mtch.group(4));

                    if(parse.get(mtch.group(1)).equals(mtch.group(3))) {
                        System.out.println("\n\n\nREPLACE");
                        page = page.replaceAll(ptrn.pattern(), escape(mtch.group(4)));
                    } else {
                        //dateMatcher.appendReplacement(page, "");
                        System.out.println("\n\n\nREMOVE - " + ptrn.pattern());
                        page = page.replaceAll(ptrn.pattern(), "");
                    }

                }

                System.out.println("\n\n\n");


            }
            return page;


}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:48:46+00:00Added an answer on May 15, 2026 at 10:48 pm

    This might be due to greedy nature.

    Try this,

    (\\p{ASCII}+?) instead of (\\p{ASCII}+)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying (and failing) to write a simple template file: <#@ template language=C# hostspecific=True
I'm trying to write a simple template system, and I have a problem. If
I trying to write a simple function to call unmanaged code from managed code.
I am trying to write some simple code which will read a text file
I'm trying to write a simple code in Java to connect to a memcache
I'm trying to write a c++ template method. Below is a sample code demonstrating
I am trying to write a simple parallel_for using pthreads. Original code is something
I'm trying to write some code for simple complex number manipulation. I'm using a
I'm trying to write simple proxy server for some purpose. In it I use
I am trying to write header file. I can write simple headers like add(int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.