Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import freemarker.template.TemplateNumberModel;
import freemarker.template.TemplateScalarModel;
import freemarker.template.TemplateSequenceModel;
import freemarker.template.TemplateStringableModel;
import freemarker.template.TemplateTransformModel;
import freemarker.template._TemplateAPI;
import freemarker.template._VersionInts;
Expand Down Expand Up @@ -730,7 +731,9 @@ public boolean isEmpty() {
@Override
TemplateModel _eval(Environment env) throws TemplateException {
TemplateModel model = target.eval(env);
if (model instanceof TemplateNumberModel) {
if (model instanceof TemplateStringableModel) {
return ((TemplateStringableModel) model).evalString();
} else if (model instanceof TemplateNumberModel) {
return new NumberFormatter((TemplateNumberModel) model, env);
} else if (model instanceof TemplateDateModel) {
TemplateDateModel dm = (TemplateDateModel) model;
Expand Down
5 changes: 4 additions & 1 deletion freemarker-core/src/main/java/freemarker/core/EvalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import freemarker.template.TemplateNumberModel;
import freemarker.template.TemplateScalarModel;
import freemarker.template.TemplateSequenceModel;
import freemarker.template.TemplateStringableModel;
import freemarker.template._VersionInts;

/**
Expand Down Expand Up @@ -476,7 +477,9 @@ private static String coerceModelToTextualCommon(
Environment env)
throws TemplateModelException, InvalidReferenceException, TemplateException,
NonStringOrTemplateOutputException, NonStringException {
if (tm instanceof TemplateScalarModel) {
if (tm instanceof TemplateStringableModel) {
return modelToString(((TemplateStringableModel) tm).evalString(), exp, env);
} else if (tm instanceof TemplateScalarModel) {
return modelToString((TemplateScalarModel) tm, exp, env);
} else if (tm == null) {
if (env.isClassicCompatible()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package freemarker.template;

import freemarker.core.TemplateMarkupOutputModel;

/**
* Data type that supports the {@code ?string} built-in.
*/
public interface TemplateStringableModel extends TemplateModel {

/**
* Returns a string representation of the current value.
*/
public TemplateScalarModel evalString() throws TemplateModelException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package freemarker.core;

import java.io.IOException;
import java.util.Collections;

import org.junit.Before;
import org.junit.Test;

import freemarker.template.Configuration;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;
import freemarker.template.TemplateStringableModel;
import freemarker.test.TemplateTest;

public class StringifiableTest extends TemplateTest {

private static class Stringable implements TemplateStringableModel {

private final String s;

Stringable(String s) { this.s = s; }

@Override public TemplateScalarModel evalString() {
return new SimpleScalar("<" + s + ">");
}
}

@Test
public void testStringifiable() throws IOException, TemplateException {
assertOutput("${zz}", "<zz>");
assertOutput("${zz?string}", "<zz>");
assertErrorContains("${yy}", "misc_template_model");
}

@Before
public void setup() throws TemplateModelException {
Configuration cfg = getConfiguration();

addToDataModel("zz", new Stringable("zz"));
addToDataModel("yy", new TemplateModel() {});
}

}