Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id AB523200D0F for ; Fri, 29 Sep 2017 14:29:05 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id A7ECA1609D1; Fri, 29 Sep 2017 12:29:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id ED2C71609C5 for ; Fri, 29 Sep 2017 14:29:04 +0200 (CEST) Received: (qmail 48917 invoked by uid 500); 29 Sep 2017 12:29:04 -0000 Mailing-List: contact notifications-help@freemarker.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@freemarker.incubator.apache.org Delivered-To: mailing list notifications@freemarker.incubator.apache.org Received: (qmail 48908 invoked by uid 99); 29 Sep 2017 12:29:04 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Sep 2017 12:29:04 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id A21F21A2FC8 for ; Fri, 29 Sep 2017 12:29:03 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -99.202 X-Spam-Level: X-Spam-Status: No, score=-99.202 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id 7y9SN3_KIwen for ; Fri, 29 Sep 2017 12:29:02 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id 1E90C5F3CF for ; Fri, 29 Sep 2017 12:29:02 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 8653AE06CF for ; Fri, 29 Sep 2017 12:29:01 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id 45A65242AB for ; Fri, 29 Sep 2017 12:29:01 +0000 (UTC) Date: Fri, 29 Sep 2017 12:29:01 +0000 (UTC) From: "Daniel Dekany (JIRA)" To: notifications@freemarker.incubator.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Comment Edited] (FREEMARKER-75) New built-in ?as("variableName") to store the left-hand operand to a variable. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Fri, 29 Sep 2017 12:29:05 -0000 [ https://issues.apache.org/jira/browse/FREEMARKER-75?page=3Dcom.atlass= ian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=3D1= 6185750#comment-16185750 ]=20 Daniel Dekany edited comment on FREEMARKER-75 at 9/29/17 12:28 PM: ------------------------------------------------------------------- Regarding that {{?as}} should replace an existing variable. Maybe the sane = way of combining this with FM2 is that {{?as\(x)}} declares a nested conten= t variable (aka a loop variable), similarly to {{#list xs as x}} does. Othe= rwise the scope is not obvious, like, if you have {{$\{expensiveExpression?= as\(x)\}}} inside {{#function}}, will {{x}} be a local variable, or a templ= ate namespace variable? (In FM3 I plan to use {{#var}} and {{#set}}, where = the scope of the last depends on where {{#var}} was.) Anyway, this last doe= sn't exactly helps the readability of the template ("Where does x coming fr= om?" Looks for {{#assign}}-s for no avail...), so I probably wouldn't do th= at it in FM3 either. The case where {{?as\(x)}} declares a nested content v= ariable has a strong use case with {{#if}}, and the scope of the variable i= s restricted, which hopefully makes this less confusing. was (Author: ddekany): Regarding that {{?as}} should replace an existing variable. Maybe the sane = way of combining this with FM2 is that {{?as(x)}} declares a nested content= variable (aka a loop variable), similarly to {{#list xs as x}} does. Other= wise the scope is not obvious, like, if you have {{$\{expensiveExpression?a= s(x)\}}} inside {{#function}}, will {{x}} be a local variable, or a templat= e namespace variable? (In FM3 I plan to use {{#var}} and {{#set}}, where th= e scope of the last depends on where {{#var}} was.) Anyway, this last doesn= 't exactly helps the readability of the template ("Where does x coming from= ?" Looks for {{#assign}}-s for no avail...), so I probably wouldn't do that= it in FM3 either. The case where {{?as(x)}} declares a nested content vari= able has a strong use case with {{#if}}, and the scope of the variable is r= estricted, which hopefully makes this less confusing. > New built-in ?as("variableName") to store the left-hand operand to a vari= able. > -------------------------------------------------------------------------= ----- > > Key: FREEMARKER-75 > URL: https://issues.apache.org/jira/browse/FREEMARKER-75 > Project: Apache Freemarker > Issue Type: New Feature > Components: engine > Affects Versions: 2.3.26-incubating > Reporter: Ondra =C5=BDi=C5=BEka > > When coding templates, one often runs into a situation like this: > {code} > <#if (foo.bar.baz.expensiveCall())!false > > ${ foo.bar.baz.expensiveCall() } > > {code} > Solving it with currently available things is something like=20 > {code} > <#assign cached =3D (foo.bar.baz.expensiveCall())! > > <#if cached?has_content() > > Hello, ${ cached } > > {code} > In short, passing around the information that a value could not be obtain= ed is a bit cumbersome. > What I suggest is a built-in that would capture a value of an expression = on the left to a new variable and pass it along. > {code} > <#if (foo.bar.baz.expensiveCall())!?as("cached")?has_content > > Hello, ${ cached } > > {code} > The benefits is that there is need to compute the expression twice or do = the assignment. > Speaking of simplifying, the expression above could also be simplified by= another built-in, a "reversed" {{then()}}. (For that I will file another = jira.) > {code} > ${ "Hi, "?if( expensive()?as("cached"), "") + cached } // For conditi= onal prefixes > ${ expensive()?as("cached")!false + ", good bye!"?if(cached) } // For= conditional suffixes > ${ "Hi, ${expensive()?as("cached")!false}, again!"?if(cached) } // Fo= r conditional surrounding > {code} > The new {{?as()}} built-in could be also used for a more convenient way o= f assigning wile printing: > {code} > ${"Hi ${user}"?as("greet")}. > ${ greet }, again. > {code} > I hope my explanation does make sense. > Thanks for considering.=20 -- This message was sent by Atlassian JIRA (v6.4.14#64029)