您好,我想为从 xml 请求收到的数据创建一个(内部或外部)html 模板。这是我的代码,目前运行良好:
NSString* desc = [screenData.jsonVars objectForKey:@"descriptionTXT"];
NSString* title = [screenData.jsonVars objectForKey:@"titleTXT"];
NSString* day = [screenData.jsonVars objectForKey:@"dayTXT"];
NSString* month = [screenData.jsonVars objectForKey:@"monthTXT"];
NSString* url = [screenData.jsonVars objectForKey:@"dataURL"];
NSString* htmlContentString = [NSString stringWithFormat:
@"<html>"
"<style type=\"text/css\">"
"body { background-color:transparent; font-family:Marker Felt; font-size:44;color:#fff;}"
"</style>"
"<body>"
"<p style=\"text-align:center;font-size:65px;\">%@</p>"
"<div style=\"color:#ff9900;margin:5px;padding:10px;\">%@ %@</div>"
"<div style=\"color:#000;background:#DBDBDB;margin:5px;padding:10px;\">%@</div>"
"<div style=\"color:#000;background:#ff9900;margin:5px;padding:10px;\"><a href=\"%@\">Go to website</a></div>"
"</body></html>", title, day, month, desc, url];
[BT_debugger showIt:self:[NSString stringWithFormat:@"This is the HTML: %@", htmlContentString]];
[self.webView loadHTMLString:htmlContentString baseURL:nil];
[self.view addSubview:webView];
现在我想取出 html 并从外部或内部文件中读取它。例如在 template1.html
如您所见,它是动态的(它是数据),我希望保持这种状态。
任何人对此有建议 提前致谢,
D.
最佳答案
使用 +stringByReplacingOccurencesOfString: 和 NSMutableString 的答案应该使用 -replaceOccurrencesOfString: 代替。此外,NSBundle -pathForResource: 返回一个路径,而不是文件的实际内容。这是一个有效的示例:
模板
<!-- template.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<style type="text/css">
body{
font-family:"Helvetica Neue UltraLight",helvetica,verdana;
margin-left:0;
margin-right:0;
}
section{
padding-left:5px;
padding-right:5px;
font-size:12px;
}
h1{
font-size: 16px;
padding-left:5px;
}
img{
display: block;
/* 4:3 dimensions */
width: 220px;
height:165px;
}
</style>
</head>
<body>
<h1>[[[name]]]</h1>
<img src="[[[image]]]" alt="" />
<section>
<p>[[[full_description]]]</p>
</section>
</body>
</html>
代码
// load the template
NSString *path = [[NSBundle mainBundle] pathForResource:@"template" ofType:@"html"];
NSString *template = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSMutableString *html = [NSMutableString stringWithString:template];
// make substitutions
[html replaceOccurrencesOfString:@"[[[name]]]" withString:self.building.name options:NSLiteralSearch range:NSMakeRange(0, html.length)];
[html replaceOccurrencesOfString:@"[[[image]]]" withString:self.building.image options:NSLiteralSearch range:NSMakeRange(0, html.length)];
[html replaceOccurrencesOfString:@"[[[full_description]]]" withString:self.building.fulldescription options:NSLiteralSearch range:NSMakeRange(0, html.length)];
// load html string into webView
[self.webview loadHTMLString:html baseURL:nil];
关于iphone - UIWebView动态html转模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9669241/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'
我正在使用Mandrill的RubyAPIGem并使用以下简单的测试模板:testastic按照Heroku指南中的示例,我有以下Ruby代码:require'mandrill'm=Mandrill::API.newrendered=m.templates.render'test-template',[{:header=>'someheadertext',:main_section=>'Themaincontentblock',:footer=>'asdf'}]mail(:to=>"JaysonLane",:subject=>"TestEmail")do|format|format.h
我正在尝试将一个简单的CSV文件读入HTML表格以在浏览器中显示,但我遇到了麻烦。这就是我正在尝试的:Controller:defshow@csv=CSV.open("file.csv",:headers=>true)end查看:输出:NameStartDateEndDateQuantityPostalCode基本上我只获取标题,而不会读取和呈现CSV正文。 最佳答案 这最终成为最终解决方案:Controller:defshow#OpenaCSVfile,andthenreaditintoaCSV::Tableobjectforda
所以这可能有点令人困惑,但请耐心等待。简而言之,我想遍历具有特定键值的所有属性,然后如果值不为空,则将它们插入到模板中。这是我的代码:属性:#===DefaultfileConfigurations#default['elasticsearch']['default']['ES_USER']=''default['elasticsearch']['default']['ES_GROUP']=''default['elasticsearch']['default']['ES_HEAP_SIZE']=''default['elasticsearch']['default']['MAX_OP
有没有办法在Ruby中动态创建数组?例如,假设我想遍历用户输入的书籍数组:books=gets.chomp用户输入:"TheGreatGatsby,CrimeandPunishment,Dracula,Fahrenheit451,PrideandPrejudice,SenseandSensibility,Slaughterhouse-Five,TheAdventuresofHuckleberryFinn"我把它变成一个数组:books_array=books.split(",")现在,对于用户输入的每一本书,我想用Ruby创建一个数组。伪代码来做到这一点:x=0books_array.